⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wea_anal.c

📁 解压后请将文件扩展名改为.C,实现的是CVI里的数据库操作.
💻 C
字号:
/*---------------------------------------------------------------------------*/
/* SQL Weather Example Program - Data Analysis                               */
/*---------------------------------------------------------------------------*/
/* define CVICALLBACK so example programs work under CVI 3.1*/
#if !defined(_NI_mswin32_) && !defined(_WIN32)
    #if (!defined(_CVI_)) || (defined(_CVI_) && ((_CVI_==1) || (_CVI_==301) || (_CVI_==310)))
        #ifndef CVICALLBACK
            #define CVICALLBACK
        #endif
    #endif    
#endif

#include "cvi_db.h"
#include <formatio.h>
#include <utility.h>
#include <ansi_c.h>
#include <userint.h>
#include "weather.h"


/* Global variables with Data Source information */
extern char dsnName[32];
extern char tableName[16];

extern int mainPanel;

/* Analysis panel and menubar handles */
static int aTempPanel, aWindPanel, aSunPanel, aPrecipPanel, menuBarHndl;
static int aMainPanel, aCurrentChild;


/*Handle for SQL Commands and Current Panel*/
static int hdbc, hstmt;


/*---------------------------------------------------------------------------*/
/* Main procedure for Data Analysis                                          */
/*---------------------------------------------------------------------------*/
int CVICALLBACK Analyze (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{

    int status;

    /* Displays graphical analysis panel and load child panels */
    switch (event) {
    case EVENT_COMMIT:
        aMainPanel   = LoadPanel (0, "weather.uir", AMAIN);
        menuBarHndl  = LoadMenuBar (aMainPanel, "weather.uir", MENUBAR);
        aTempPanel   = LoadPanel (aMainPanel, "weather.uir", ATEMP);
        aWindPanel   = LoadPanel (aMainPanel, "weather.uir", AWIND);
        aSunPanel    = LoadPanel (aMainPanel, "weather.uir", ASUN);
        aPrecipPanel = LoadPanel (aMainPanel, "weather.uir", APRECIP);

        /* Check specific values in checkbox as default */
        status = CheckListItem (aPrecipPanel, APRECIP_LISTBOX, 1, 1);
        status = CheckListItem (aPrecipPanel, APRECIP_LISTBOX, 2, 1);
        status = CheckListItem (aTempPanel, ATEMP_LISTBOX, 0, 1);

        HidePanel(mainPanel);

        DisplayPanel(aMainPanel);
        DisplayPanel(aPrecipPanel);
        aCurrentChild=aPrecipPanel;

    break;
    }
    return 0;
}


void CVICALLBACK Done(int menubar, int menuItem, void *callbackData, int panel)
{
  /*Quit graphical analysis panel and return to main*/
  SetCtrlAttribute (aMainPanel, AMAIN_PLOT, ATTR_DIMMED, 1);
  DeleteGraphPlot (aMainPanel, AMAIN_GRAPH, -1, VAL_IMMEDIATE_DRAW);

  DiscardPanel(aTempPanel);
  DiscardPanel(aWindPanel);
  DiscardPanel(aSunPanel);
  DiscardPanel(aPrecipPanel);
  DiscardPanel(aMainPanel);

  DisplayPanel(mainPanel);
}


/*---------------------------------------------------------------------------*/
/* Plot the selected data to the main panel graph                            */
/*---------------------------------------------------------------------------*/
int CVICALLBACK Plot (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    /*Plots selected data to the graph*/
    /*Weather varibles*/
    static char date[12];
    static double Temp,HighTemp,LowTemp,Precip,Rain;
    static double WindSpeed,Snow,SunShine;
    static int StatT,StatP,StatR,StatW,StatS,StatSun;
    static int StatTL,StatTH,StatD;
    static char startdate[12],stopdate[12];
    int srtmonth,srtday,srtyear,stpmonth,stpday,stpyear,monthcnt;
    int check1,check2,check3;
    int Day_In_Month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    /*Control Varibles*/
    int resCode,Plot_Handle,Num_Days;
    static char sqlstr[256];
    char *le = "<=";
    char *ge = ">=";
    /*Plotdata*/
    static double dataarrayY[365],dataarrayY1[365],dataarrayY2[365];
    static double sumdata,sumdata1,sumdata2;
    static int dataarrayX[365],i;

    switch (event) {
    case EVENT_COMMIT:

      /*Get start and stop date*/
      GetCtrlVal(aMainPanel,AMAIN_STARTDATE,startdate);
      GetCtrlVal(aMainPanel,AMAIN_STOPDATE,stopdate);

      Scan(startdate,"%s>%d[x]%d[x]%d[x]", &srtyear,&srtmonth,&srtday);
      Scan(stopdate,"%s>%d[x]%d[x]%d[x]",&stpyear, &stpmonth,&stpday);
      Fmt(startdate,"%s<%d[w4p0]/%d[w2p0]/%d[w2p0]",srtyear,srtmonth,srtday);
      Fmt(stopdate, "%s<%d[w4p0]/%d[w2p0]/%d[w2p0]",srtyear,stpmonth,stpday);

      for (monthcnt=1;monthcnt<srtmonth;monthcnt++)
        srtday += Day_In_Month[monthcnt];
      for (monthcnt=1;monthcnt<stpmonth;monthcnt++)
        stpday += Day_In_Month[monthcnt];

      if (srtday >= stpday) {MessagePopup ("Invalid Dates",
                                           "The stop date must be later than\nthe start date please fix and try again.");return 0;}
      Num_Days = stpday-srtday+1;

      /*Connect to database driver and activate database*/
      hdbc = DBConnect (dsnName);
        if (hdbc <= 0) {MessagePopup ("SQL ERROR",DBErrorMessage ()); RemovePopup(1);return 0;}

      Fmt(sqlstr,"%s<SELECT * FROM %s WHERE W_DATE %s '%s' AND W_DATE %s '%s' ORDER BY W_DATE",tableName,ge,startdate,le,stopdate);
      hstmt = DBActivateSQL (hdbc, sqlstr);
        if (hstmt == 0) {MessagePopup ("SQL ERROR",DBErrorMessage ());return 0;}

       /*Map Columns to specified formats and link to varible*/
      resCode = DBBindColChar (hstmt, 1, 12, date, &StatD, "");
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 2, &LowTemp, &StatTL);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 3, &HighTemp, &StatTH);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 4, &Temp, &StatT);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 5, &Rain, &StatR);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 6, &Snow, &StatS);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 7, &Precip, &StatP);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 8, &WindSpeed, &StatW);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}
      resCode = DBBindColDouble (hstmt, 9, &SunShine, &StatSun);
        if (resCode != DB_SUCCESS) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}

      /*Modify Graph axis*/
      SetAxisRange (aMainPanel, AMAIN_GRAPH, VAL_MANUAL, srtday, stpday,
                    VAL_NO_CHANGE, 0.0, 1.0);
          /*assamble data to plot*/
          i=0;
          sumdata=0;
          sumdata1=0;
          sumdata2=0;
      /*Scroll through records with DBFetchNext*/
      while ((resCode = DBFetchNext (hstmt)) == DB_SUCCESS) {
         if (aCurrentChild == aTempPanel){
          dataarrayY[i] = Temp;
          dataarrayY1[i] = HighTemp;
          dataarrayY2[i] = LowTemp;
          sumdata +=Temp;
          sumdata1 += HighTemp;
          sumdata2 += LowTemp;
         }
         else if (aCurrentChild == aWindPanel){
          dataarrayY[i] = WindSpeed;
          sumdata += WindSpeed;
         }
         else if (aCurrentChild == aSunPanel){
          dataarrayY[i] = SunShine;
          sumdata += SunShine;
         }
         else{
          dataarrayY[i] = Precip;
          dataarrayY1[i] = Rain;
          dataarrayY2[i] = Snow;
          sumdata += Precip;
          sumdata1 += Rain;
          sumdata2 += Snow;
         }
          dataarrayX[i] = srtday;
          srtday++;
          i++;
      }
      sumdata /= Num_Days;
      sumdata1 /= Num_Days;
      sumdata2 /= Num_Days;

      DeleteGraphPlot (aMainPanel, AMAIN_GRAPH, -1, VAL_IMMEDIATE_DRAW);

      if (aCurrentChild == aTempPanel){
       IsListItemChecked (aTempPanel, ATEMP_LISTBOX, 0, &check1);
       IsListItemChecked (aTempPanel, ATEMP_LISTBOX, 1, &check2);
       IsListItemChecked (aTempPanel, ATEMP_LISTBOX, 2, &check3);
       if (check1 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY1, Num_Days,
                        VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE,
                        VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
       if (check2 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY2, Num_Days,
                        VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE,
                        VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_DK_GREEN);
       if (check3 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY, Num_Days,
                        VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE,
                        VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);

       SetCtrlVal (aTempPanel, ATEMP_TEMP, sumdata);
       SetCtrlVal (aTempPanel, ATEMP_HITEMP, sumdata1);
       SetCtrlVal (aTempPanel, ATEMP_LOWTEMP, sumdata2);
     }
      else if (aCurrentChild == aPrecipPanel){
       IsListItemChecked (aPrecipPanel, APRECIP_LISTBOX, 0, &check1);
       IsListItemChecked (aPrecipPanel, APRECIP_LISTBOX, 1, &check2);
       IsListItemChecked (aPrecipPanel, APRECIP_LISTBOX, 2, &check3);
       if (check1 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY,
                       Num_Days, VAL_INTEGER, VAL_DOUBLE,
                       VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1,
                       VAL_BLUE);
       if (check2 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY1, Num_Days,
                        VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE,
                        VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_DK_GREEN);
       if (check3 == 1)
         Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY2, Num_Days,
                        VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE,
                        VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);

       SetCtrlVal (aPrecipPanel, APRECIP_PRECIP, sumdata*Num_Days);
       SetCtrlVal (aPrecipPanel, APRECIP_RAIN, sumdata1*Num_Days);
       SetCtrlVal (aPrecipPanel, APRECIP_SNOW, sumdata2*Num_Days);
     }
     else if (aCurrentChild == aWindPanel){
       SetCtrlVal (aWindPanel,AWIND_WINDMETER,sumdata);
       Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY,
                             Num_Days, VAL_INTEGER, VAL_DOUBLE,
                             VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID,
                             1, VAL_RED);
     }
     else if (aCurrentChild == aSunPanel){
       SetCtrlVal (aSunPanel,ASUN_SUN,sumdata);
       Plot_Handle = PlotXY (aMainPanel, AMAIN_GRAPH, dataarrayX, dataarrayY,
                             Num_Days, VAL_INTEGER, VAL_DOUBLE,
                             VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID,
                             1, VAL_RED);
     }
      /*Disconnect database */
      resCode = DBDisconnect (hdbc);
        if (resCode != 0) {MessagePopup ("SQL ERROR",DBErrorMessage ()); return 0;}

    break;
    }
    return 0;
}


/*---------------------------------------------------------------------------*/
/* Callback for Wind Menu Item - Display the Wind Child Panel                */
/*---------------------------------------------------------------------------*/
void CVICALLBACK WindDisp(int menubar, int menuItem, void *callbackData, int panel)
{

    /*Load associated panel to display data*/
    HidePanel(aCurrentChild);

    DisplayPanel(aWindPanel);
    aCurrentChild = aWindPanel;

    SetCtrlAttribute (aMainPanel, AMAIN_GRAPH, ATTR_YNAME, "Wind Speed [MPH]");
}


/*---------------------------------------------------------------------------*/
/* Callback for Precipitation Menu Item - Display the Precip Child Panel     */
/*---------------------------------------------------------------------------*/
void CVICALLBACK PrecipDisp(int menubar, int menuItem, void *callbackData, int panel)
{
    /*Load associated panel to display data*/
    HidePanel(aCurrentChild);

    DisplayPanel(aPrecipPanel);
    aCurrentChild = aPrecipPanel;

    SetCtrlAttribute (aMainPanel, AMAIN_GRAPH, ATTR_YNAME, "Precipitation [inches]");
}


/*---------------------------------------------------------------------------*/
/* Callback for Temperature Menu Item - Display the Temperature Child Panel  */
/*---------------------------------------------------------------------------*/
void CVICALLBACK TempDisp(int menubar, int menuItem, void *callbackData, int panel)
{
    /*Load associated panel to display data*/
    HidePanel(aCurrentChild);

    DisplayPanel(aTempPanel);
    aCurrentChild = aTempPanel;

    SetCtrlAttribute (aMainPanel, AMAIN_GRAPH, ATTR_YNAME, "Temperature [degree F]");
}


/*---------------------------------------------------------------------------*/
/* Callback for Sunshine Menu Item - Display the Sunshine Child Panel        */
/*---------------------------------------------------------------------------*/
void CVICALLBACK SunDisp(int menubar, int menuItem, void *callbackData, int panel)
{
    /*Load associated panel to display data*/
    HidePanel(aCurrentChild);

    DisplayPanel(aSunPanel);
    aCurrentChild = aSunPanel;

    SetCtrlAttribute (aMainPanel, AMAIN_GRAPH, ATTR_YNAME, "Percent");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -