阅读:1361回复:0
C++ Example of an Unbound Dataset
C++ Example of an Unbound Dataset
/* This sample demonstrates the use of Datasets.Add and the * RequestData event supported by MapX to build an unbound * dataset, which allows MapX to access data whose format * is known only to the programmer. In this example, the * data is in the form of an array of structs in C++. */ // For help on handling events in C++, see "Handling MapX Events // using C++" in the "Working with Visual C++" chapter. BEGIN_EVENTSINK_MAP(CMapXView, CView) : ON_EVENT(CMapXView, IDC_MAP, MAPX_DISPID_REQUESTDATA, OnRequestData, VTS_BSTR VTS_I4 VTS_I2 VTS_PVARIANT VTS_PBOOL) : END_EVENTSINK_MAP() : : : const int kNumberOfRows = 3; struct { char* stateName; double sales; } theData[kNumberOfRows] = { {"NY", 100}, {"NH", 200}, {"VT", 300} }; void CMapXView::OnUnboundClick() { CMapXFields flds; CMapXDataset ds; COleVariant fldsVt; COptionalVariant optVt; try { // Zoom in on New England m_ctrlMapX.ZoomTo(800, -70.26, 44.05); // Create a new Fields object flds.CreateDispatch(flds.GetClsid()); // Describe the structure of the unbound dataset: flds.Add(COleVariant("State"), COleVariant("State"), COleVariant((short)miAggregationIndividual), COleVariant((short)miTypeString)); flds.Add(COleVariant("Sales"), COleVariant("Sales"), COleVariant((short)miAggregationSum), COleVariant((short)miTypeNumeric)); // Point the fldsVt variant at our new Fields object fldsVt.vt = VT_DISPATCH; fldsVt.pdispVal = flds.m_lpDispatch; fldsVt.pdispVal->AddRef(); // Create the unbound dataset. The "RequestData" event // will be triggered to get the data to be used. ds = m_ctrlMapX.GetDatasets().Add(miDataSetUnbound, optVt, COleVariant("My Dataset"), COleVariant("State"), optVt, COleVariant("USA"), fldsVt, optVt); // Create a theme based on the "Sales" column in the // unbound dataset ds.GetThemes().Add(miThemeRanged, "Sales", "My Theme"); } catch (COleDispatchException *e) { e->ReportError(); e->Delete(); } catch (COleException *e) { e->ReportError(); e->Delete(); } } void CMapXView::OnRequestData(LPCTSTR DataSetName, long Row, short Field, VARIANT FAR* value, BOOL FAR* Done) { *Done = false; // Tell MapX to continue fetching data unless we've exceeded // the bounds of the dataset if(strcmp(DataSetName, "My Dataset") == 0 && Row > 0 && Row <= kNumberOfRows) { switch(Field) { case 1: // State field // copy the state string into the value variant. value->vt = VT_BSTR; value->bstrVal = CString(theData[Row-1].stateName).AllocSysString(); break; case 2: // Sales field // copy the sales double into the value variant value->vt = VT_R8; value->dblVal = theData[Row-1].sales; break; } } else // Gone out of bounds... tell MapX to stop fetching *Done = true; } |
|
|