// // Draw the chart // private void drawChart(WPFChartViewer viewer) { // Have not started collecting data ??? if (currentIndex <= 0) { return; } // The start time is equal to the latest time minus the time range of the chart double startTime = timeStamps[currentIndex - 1] - timeRange; int startIndex = (int)Math.Ceiling(Chart.bSearch(timeStamps, 0, currentIndex, startTime) - 0.1); // For a sweep chart, if the line goes beyond the right border, it will wrap back to // the left. We need to determine the wrap position (the right border). double wrapTime = Math.Floor(startTime / timeRange + 1) * timeRange; double wrapIndex = Chart.bSearch(timeStamps, 0, currentIndex, wrapTime); int wrapIndexA = (int)Math.Ceiling(wrapIndex); int wrapIndexB = (int)Math.Floor(wrapIndex); // The data arrays and the colors and names of the data series var allArrays = new[] { timeStamps, channel1, channel2 }; int[] colors = { 0xff0000, 0x00cc00 }; string[] names = { "Channel 1", "Channel 2" }; // Split all data arrays into two parts A and B at the wrap position. The B part is the // part that is wrapped back to the left. var allArraysA = new double[allArrays.Length][]; var allArraysB = new double[allArrays.Length][]; for (int i = 0; i < allArrays.Length; ++i) { allArraysA[i] = (double[])Chart.arraySlice(allArrays[i], startIndex, wrapIndexA - startIndex + 1); allArraysB[i] = (double[])Chart.arraySlice(allArrays[i], wrapIndexB, currentIndex - wrapIndexB); } // Normalize the plotted timeStamps (the first element of allArrays) to start from 0 for (int i = 0; i < allArraysA[0].Length; ++i) { allArraysA[0][i] -= wrapTime - timeRange; } for (int i = 0; i < allArraysB[0].Length; ++i) { allArraysB[0][i] -= wrapTime; } // // Now we have prepared all the data and can plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object the same size as WPFChartViewer, with a minimum of 300 x 150 XYChart c = new XYChart(Math.Max(300, (int)viewer.ActualWidth), Math.Max(150, (int)viewer.ActualHeight)); // Set the plotarea at (0, 0) with width 1 pixel less than chart width, and height 20 pixels // less than chart height. Use a vertical gradient from light blue (f0f6ff) to sky blue (a0c0ff) // as background. Set border to transparent and grid lines to white (ffffff). c.setPlotArea(0, 0, c.getWidth() - 1, c.getHeight() - 20, c.linearGradientColor(0, 0, 0, c.getHeight() - 20, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // In our code, we can overdraw the line slightly, so we clip it to the plot area. c.setClipping(); // Add a legend box at the right side using horizontal layout. Use 10pt Arial Bold as font. Set // the background and border color to Transparent and use line style legend key. LegendBox b = c.addLegend(c.getWidth() - 1, 10, false, "Arial Bold", 10); b.setBackground(Chart.Transparent); b.setAlignment(Chart.Right); b.setLineStyleKey(); // Set the x and y axis stems to transparent and the label font to 10pt Arial c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); c.xAxis().setLabelStyle("Arial", 10); c.yAxis().setLabelStyle("Arial", 10, 0x336699); // Configure the y-axis label to be inside the plot area and above the horizontal grid lines c.yAxis().setLabelGap(-1); c.yAxis().setMargin(20); c.yAxis().setLabelAlignment(1); // Configure the x-axis labels to be to the left of the vertical grid lines c.xAxis().setLabelAlignment(1); //================================================================================ // Add data to chart //================================================================================ // Draw the lines, which consists of A segments and B segments (the wrapped segments) foreach (var dataArrays in new[] { allArraysA, allArraysB }) { LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); layer.setFastLineMode(); // The first element of dataArrays is the timeStamp, and the rest are the data. layer.setXData(dataArrays[0]); for (int i = 1; i < dataArrays.Length; ++i) { layer.addDataSet(dataArrays[i], colors[i - 1], names[i - 1]); } // Disable legend entries for the B lines to avoid duplication with the A lines if (dataArrays == allArraysB) { layer.setLegend(Chart.NoLegend); } } // The B segments contain the latest data. We add a vertical line at the latest position. int lastIndex = allArraysB[0].Length - 1; Mark m = c.xAxis().addMark(allArraysB[0][lastIndex], -1); m.setMarkColor(0x0000ff, Chart.Transparent, Chart.Transparent); m.setDrawOnTop(false); // We also add a symbol and a label for each data series at the latest position for (int i = 1; i < allArraysB.Length; ++i) { // Add the symbol Layer layer = c.addScatterLayer(new double[] { allArraysB[0][lastIndex] }, new double[] { allArraysB[i][lastIndex] }, "", Chart.CircleSymbol, 9, colors[i - 1], colors[i - 1]); layer.moveFront(); // Add the label string label = "<*font,bgColor=" + colors[i - 1].ToString("x") + "*> {value|P4} <*/font*>"; layer.setDataLabelFormat(label); // The label style ChartDirector.TextBox t = layer.setDataLabelStyle("Arial Bold", 10, 0xffffff); bool isOnLeft = allArraysB[0][lastIndex] <= timeRange / 2; t.setAlignment(isOnLeft ? Chart.Left : Chart.Right); t.setMargin(isOnLeft ? 5 : 0, isOnLeft ? 0 : 5, 0, 0); } //================================================================================ // Configure axis scale and labelling //================================================================================ c.xAxis().setLinearScale(0, timeRange); // For the automatic axis labels, set the minimum spacing to 75/40 pixels for the x/y axis. c.xAxis().setTickDensity(75); c.yAxis().setTickDensity(40); // Set the auto-scale margin to 0.05, and the zero affinity to 0.6 c.yAxis().setAutoScale(0.05, 0.05, 0.6); //================================================================================ // Output the chart //================================================================================ viewer.Chart = c; }
//Main code for creating chart. //Note: the argument chartIndex is unused because this demo only has 1 chart. public void createChart(WinChartViewer viewer, int chartIndex) { // The data for the 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, 84, 89 }; // The error data representing the error band around the data points double[] errData = { 5, 6, 5.1, 6.5, 6.6, 8, 5.4, 5.1, 4.6, 5.0, 5.2, 6.0, 4.9, 5.6, 4.8, 6.2, 7.4, 7.1, 6.5, 9.6, 12.1, 15.3, 18.5, 20.9, 24.1 }; // The timestamps for the data DateTime[] labels = { new DateTime(2001, 1, 1), new DateTime(2001, 2, 1), new DateTime( 2001, 3, 1), new DateTime(2001, 4, 1), new DateTime(2001, 5, 1), new DateTime(2001, 6, 1), new DateTime(2001, 7, 1), new DateTime(2001, 8, 1),new DateTime(2001,9, 1), new DateTime(2001, 10, 1), new DateTime(2001, 11, 1), new DateTime(2001, 12, 1), new DateTime(2002, 1, 1), new DateTime(2002, 2, 1), new DateTime(2002, 3, 1), new DateTime(2002, 4, 1), new DateTime(2002, 5, 1), new DateTime(2002, 6, 1), new DateTime(2002, 7, 1), new DateTime(2002, 8, 1), new DateTime(2002, 9, 1), new DateTime(2002, 10, 1), new DateTime(2002, 11, 1), new DateTime(2002, 12, 1), new DateTime(2003, 1, 1) }; // Create a XYChart object of size 550 x 220 pixels XYChart c = new XYChart(550, 220); // Set the plot area at (50, 10) and of size 480 x 180 pixels. Enabled both vertical and // horizontal grids by setting their colors to light grey (cccccc) c.setPlotArea(50, 10, 480, 180).setGridColor(0xcccccc, 0xcccccc); // Add a legend box (50, 10) (top of plot area) using horizontal layout. Use 8pt Arial // font. Disable bounding box (set border to transparent). LegendBox legendBox = c.addLegend(50, 10, false, "", 8); legendBox.setBackground(Chart.Transparent); // Add keys to the legend box to explain the color zones legendBox.addKey("Historical", 0x9999ff); legendBox.addKey("Forecast", 0xff9966); // Add a title to the y axis. c.yAxis().setTitle("Energy Consumption"); // Set the labels on the x axis c.xAxis().setLabels2(labels); // Set multi-style axis label formatting. Use Arial Bold font for yearly labels and // display them as "yyyy". Use default font for monthly labels and display them as // "mmm". Replace some labels with minor ticks to ensure the labels are at least 3 units // apart. c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*font=Arial Bold*>{value|yyyy}", Chart.StartOfMonthFilter(), "{value|mmm}", 3); // Add a line layer to the chart LineLayer layer = c.addLineLayer2(); // Create the color to draw the data line. The line is blue (0x333399) to the left of x // = 18, and become a red (0xd04040) dash line to the right of x = 18. int lineColor = layer.xZoneColor(18, 0x333399, c.dashLineColor(0xd04040, Chart.DashLine) ); // Add the data line layer.addDataSet(data, lineColor, "Average"); // We are not showing the data set name in the legend box. The name is for showing in // tool tips only. layer.setLegend(Chart.NoLegend); // Create the color to draw the err zone. The color is semi-transparent blue // (0x809999ff) to the left of x = 18, and become semi-transparent red (0x80ff9966) to // the right of x = 18. int errColor = layer.xZoneColor(18, unchecked ((int)0x809999ff), unchecked ((int)0x80ff9966)); // Add the upper border of the err zone layer.addDataSet(new ArrayMath(data).add(errData).result(), errColor, "Upper bound"); // Add the lower border of the err zone layer.addDataSet(new ArrayMath(data).sub(errData).result(), errColor, "Lower bound"); // Set the default line width to 2 pixels layer.setLineWidth(2); // In this example, we are not showing the data set name in the legend box layer.setLegend(Chart.NoLegend); // Color the region between the err zone lines c.addInterLineLayer(layer.getLine(1), layer.getLine(2), errColor); // Output the chart viewer.Chart = c; // Include tool tip for the chart. viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{dataSetName} on {xLabel|mmm yyyy}: {value} MJoule'"); }