//Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WPFChartViewer viewer, int chartIndex) { // The data for the line chart double[] data = { 50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 91 }; // The labels for the line chart string[] labels = { "Jan 2000", "Feb 2000", "Mar 2000", "Apr 2000", "May 2000", "Jun 2000", "Jul 2000", "Aug 2000", "Sep 2000", "Oct 2000","Nov 2000", "Dec 2000", "Jan 2001", "Feb 2001", "Mar 2001", "Apr 2001", "May 2001","Jun 2001", "Jul 2001", "Aug 2001", "Sep 2001", "Oct 2001", "Nov 2001", "Dec 2001" }; // Create a XYChart object of size 500 x 320 pixels, with a pale purpule (0xffccff) // background, a black border, and 1 pixel 3D border effect. XYChart c = new XYChart(500, 320, 0xffccff, 0x000000, 1); // Set the plotarea at (55, 45) and of size 420 x 210 pixels, with white background. // Turn on both horizontal and vertical grid lines with light grey color (0xc0c0c0) c.setPlotArea(55, 45, 420, 210, 0xffffff, -1, -1, 0xc0c0c0, -1); // Add a legend box at (55, 25) (top of the chart) with horizontal layout. Use 8pt Arial // font. Set the background and border color to Transparent. c.addLegend(55, 22, false, "", 8).setBackground(Chart.Transparent); // Add a title box to the chart using 13pt Times Bold Italic font. The text is white // (0xffffff) on a purple (0x800080) background, with a 1 pixel 3D border. c.addTitle("Long Term Server Load", "Times New Roman Bold Italic", 13, 0xffffff ).setBackground(0x800080, -1, 1); // Add a title to the y axis c.yAxis().setTitle("MBytes"); // Set the labels on the x axis. Rotate the font by 90 degrees. c.xAxis().setLabels(labels).setFontAngle(90); // Add a line layer to the chart LineLayer lineLayer = c.addLineLayer(); // Add the data to the line layer using light brown color (0xcc9966) with a 7 pixel // square symbol lineLayer.addDataSet(data, 0xcc9966, "Server Utilization").setDataSymbol( Chart.SquareSymbol, 7); // Set the line width to 2 pixels lineLayer.setLineWidth(2); // tool tip for the line layer lineLayer.setHTMLImageMap("", "", "title='{xLabel}: {value} MBytes'"); // Add a trend line layer using the same data with a dark green (0x008000) color. Set // the line width to 2 pixels TrendLayer trendLayer = c.addTrendLayer(data, 0x008000, "Trend Line"); trendLayer.setLineWidth(2); // tool tip for the trend layer trendLayer.setHTMLImageMap("", "", "title='Change rate: {slope|2} MBytes/per month'"); // Output the chart viewer.Chart = c; // include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable"); }
private void generateChart() { DataTable[] rawDataTables; double?yMin = null, yMax = null, y2Min = null, y2Max = null; imageMapAreas = new StringBuilder(); preSetup(); if (_rcp.Datasources.Length == 0) { drawMessage(_noDatasourcesMessage); finalizeChart(); return; } if (_start == _end) { drawMessage("There is no data available for selected options."); finalizeChart(); return; } //Get the data from the database rawDataTables = retrieveData(_rcp.Datasources, _start, _end); //Set the scale to be measured in days //Remove the auto-generated labels _chart.xAxis().setLinearScale(Chart.CTime(_timeZone.ToLocalTime(_start).Date), Chart.CTime(_timeZone.ToLocalTime(_end).Date.AddDays(1)), null); addTimestampLabels(_start, _end); for (int i = 0; i < rawDataTables.Length; i++) { DBTable tableHelper; DateTime[] timestamps; double[] values; string[] tooltips; DisplayDatasourceItem currDataItem; ConfiguredDatasource currConfiguredDatasource; Color currLineColor; string currDataName; currDataItem = _rcp.Datasources[i]; currConfiguredDatasource = _db.ORManager.Get <ConfiguredDatasource>(currDataItem.ConfiguredDatasourceId); //If the datasource doesn't exist, don't process it if (currConfiguredDatasource == null) { continue; } currDataName = currConfiguredDatasource.Name; if (rawDataTables[i].ExtendedProperties.Contains("SubTypeId")) { DatasourceSubType currSubType; int subTypeId; subTypeId = (int)rawDataTables[i].ExtendedProperties["SubTypeId"]; currSubType = _db.ORManager.Get <DatasourceSubType>(subTypeId); currDataName += " - " + currSubType.Name; } tableHelper = new DBTable(rawDataTables[i]); timestamps = tableHelper.getColAsDateTime(0); values = tableHelper.getCol(1); //Adjust the timestamps for the users time zone for (int j = 0; j < timestamps.Length; j++) { timestamps[j] = _timeZone.ToLocalTime(timestamps[j]); } //Create an array of strings that represent the tooltips tooltips = new string[timestamps.Length]; for (int j = 0; j < timestamps.Length; j++) { tooltips[j] = getDatapointTooltip(currConfiguredDatasource, timestamps[j], values[j]); } //Determine the color to use if (currDataItem.Color == null) { currLineColor = AutoLineColors[i % AutoLineColors.Length]; } else { currLineColor = (Color)currDataItem.Color; } bool useYAxis2; useYAxis2 = (rawDataTables.Length == 2 && i == 1); //Hide the axis labels when displaying 3 or more data sources if (rawDataTables.Length > 2) { _chart.yAxis().setLabelFormat(""); } Axis axis = null; //If there are more than 2 data sources, display them on different axis if (rawDataTables.Length > 2) { axis = _chart.addAxis(Chart.Left, 0); axis.setLabelFormat(""); } //Keep a running tab of the overall min and max values if (rawDataTables.Length <= 2) { if (!useYAxis2) { double?currYMin, currYMax; ChartMath.GetDataBounds(values, out currYMin, out currYMax); if (yMin == null && currYMin != null) { yMin = currYMin; } else if (currYMin != null) { yMin = Math.Min(currYMin.Value, yMin.Value); } if (yMax == null && currYMax != null) { yMax = currYMax; } else if (currYMax != null) { yMax = Math.Min(currYMax.Value, yMax.Value); } } else { double?currYMin, currYMax; ChartMath.GetDataBounds(values, out currYMin, out currYMax); if (y2Min == null && currYMin != null) { y2Min = currYMin; } else if (currYMin != null) { y2Min = Math.Min(currYMin.Value, y2Min.Value); } if (y2Max == null && currYMax != null) { y2Max = currYMax; } else if (currYMax != null) { y2Max = Math.Min(currYMax.Value, y2Max.Value); } } } if (currDataItem.ShowRaw) { LineLayer lineLayer; lineLayer = _chart.addLineLayer2(); lineLayer.addDataSet(values, Chart.CColor(currLineColor), fixLayerNameWidth(currDataName)).setDataSymbol( Chart.DiamondSymbol, 4, 0xffff80); lineLayer.setXData(timestamps); lineLayer.addExtraField(tooltips); //field0 lineLayer.setLineWidth(currDataItem.LineThickness); if (axis == null) { lineLayer.setUseYAxis2(useYAxis2); } else { lineLayer.setUseYAxis(axis); } } if (currDataItem.ShowTrendLine) { TrendLayer tl; tl = _chart.addTrendLayer(values, _chart.dashLineColor(Chart.CColor(currLineColor), Chart.DashLine), fixLayerNameWidth(currConfiguredDatasource.Name + " Trend")); tl.setLineWidth(currDataItem.LineThickness + 1); tl.setXData(timestamps); tl.addExtraField(new string[timestamps.Length]); //field0 if (axis == null) { tl.setUseYAxis2(useYAxis2); } else { tl.setUseYAxis(axis); } } if (currDataItem.ShowLowess) { SplineLayer sl; sl = _chart.addSplineLayer(new ArrayMath(values).lowess().result(), _chart.dashLineColor(Chart.CColor(currLineColor), Chart.DashLine), fixLayerNameWidth(currDataName)); sl.setLineWidth(currDataItem.LineThickness + 1); sl.setXData(timestamps); sl.addExtraField(new string[timestamps.Length]); //field0 if (axis == null) { sl.setUseYAxis2(useYAxis2); } else { sl.setUseYAxis(axis); } } } optimizePlotArea(yMin, yMax, y2Min, y2Max); //Calculate the scale of the chart based on the plotted data, //this needs to be called before drawEvents, so the tool tip //locations for the events can be calculated. _chart.layout(); drawEvents(); drawLegend(); }