//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) { // In this example, we simply use random data for the 3 data series. RanSeries r = new RanSeries(129); double[] data0 = r.getSeries(100, 100, -15, 15); double[] data1 = r.getSeries(100, 160, -15, 15); double[] data2 = r.getSeries(100, 220, -15, 15); DateTime[] timeStamps = r.getDateSeries(100, new DateTime(2014, 1, 1), 86400); // Create a XYChart object of size 600 x 400 pixels XYChart c = new XYChart(600, 400); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial font c.addTitle(" Multi-Line Chart Demonstration", "Arial", 20, 0x555555); // Set the plotarea at (70, 70) and of size 500 x 300 pixels, with transparent // background and border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 500, 300, Chart.Transparent, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 35). Use 12pt // Arial font, transparent background and border, and line style legend icon. LegendBox b = c.addLegend(70, 35, false, "Arial", 12); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey // (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y // axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Add a title to the y axis using dark grey (0x555555) 14pt Arial font c.yAxis().setTitle("Y-Axis Title Placeholder", "Arial", 14, 0x555555); // Add a line layer to the chart with 3-pixel line width LineLayer layer = c.addLineLayer2(); layer.setLineWidth(3); // Add 3 data series to the line layer layer.addDataSet(data0, 0x5588cc, "Alpha"); layer.addDataSet(data1, 0xee9944, "Beta"); layer.addDataSet(data2, 0x99bb55, "Gamma"); // The x-coordinates for the line layer layer.setXData(timeStamps); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{x|mm/dd/yyyy}] {dataSetName}: {value}'"); }
// // Create chart // private void createChart(RazorChartViewer viewer) { // The XY data of the first data series double[] dataX = { 50, 55, 37, 24, 42, 49, 63, 72, 83, 59 }; double[] dataY = { 3.6, 2.8, 2.5, 2.3, 3.8, 3.0, 3.8, 5.0, 6.0, 3.3 }; // Create a XYChart object of size 520 x 490 pixels XYChart c = new XYChart(520, 490); // Set the plotarea at (60, 40) and of size 450 x 400 pixels, with white background and a // light grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light // grey color (0xc0c0c0) c.setPlotArea(60, 40, 450, 400, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle(" Chemical X Thermal Conductivity", "Times New Roman Bold Italic", 18); // Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Thermal Conductivity (W/K)", "Arial Bold Italic", 12); c.xAxis().setTitle("Concentration (g/liter)", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.yAxis().setWidth(3); c.xAxis().setWidth(3); // Add a scatter layer using (dataX, dataY) ScatterLayer scatterLayer = c.addScatterLayer(dataX, dataY, "", Chart.GlassSphereShape, 13, 0xcc0000); // Show custom Javascript tooltip for the scatter layer scatterLayer.setHTMLImageMap("", "", "onmouseover='showDataPointToolTip({x}, {value})' onmouseout='hideToolTip()'"); // Add a trend line layer for (dataX, dataY) TrendLayer trendLayer = c.addTrendLayer2(dataX, dataY, 0xcc0000); // Set the line width to 3 pixels trendLayer.setLineWidth(3); // Add a 95% confidence band for the line trendLayer.addConfidenceBand(0.95, unchecked ((int)0x806666ff)); // Add a 95% confidence band (prediction band) for the points trendLayer.addPredictionBand(0.95, unchecked ((int)0x8066ff66)); // Show custom Javascript tooltip for the trend layer trendLayer.setHTMLImageMap("", "", "onmouseover='showTrendLineToolTip({slope}, {intercept})' onmouseout='hideToolTip()'"); // Add a legend box at (60, 35) (top of the chart) with horizontal layout. Use 10pt Arial // Bold Italic font. Set the background and border color to Transparent and use line style // legend icons. LegendBox legendBox = c.addLegend(60, 35, false, "Arial Bold Italic", 9); legendBox.setBackground(Chart.Transparent); legendBox.setLineStyleKey(true); // Add entries to the legend box legendBox.addKey("95% Line Confidence", unchecked ((int)0x806666ff)); legendBox.addKey("95% Point Confidence", unchecked ((int)0x8066ff66)); legendBox.addKey(String.Format("Trend Line: y = {0:0.0000} x + {1:0.0000}", trendLayer.getSlope(), trendLayer.getIntercept()), 0xcc0000, 3); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap(""); // Output Javascript chart model to the browser to suppport tracking cursor viewer.ChartModel = c.getJsChartModel(); }
// // Draw the chart. // private void drawChart(WinChartViewer viewer) { // Get the start date and end date that are visible on the chart. double viewPortStartDate = viewer.getValueAtViewPort("x", viewer.ViewPortLeft); double viewPortEndDate = viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth); // Extract the part of the data arrays that are visible. double[] viewPortTimeStamps = null; double[] viewPortDataSeriesA = null; double[] viewPortDataSeriesB = null; if (currentIndex > 0) { // Get the array indexes that corresponds to the visible start and end dates int startIndex = (int)Math.Floor(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortStartDate)); int endIndex = (int)Math.Ceiling(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortEndDate)); int noOfPoints = endIndex - startIndex + 1; // Extract the visible data viewPortTimeStamps = (double[])Chart.arraySlice(timeStamps, startIndex, noOfPoints); viewPortDataSeriesA = (double[])Chart.arraySlice(dataSeriesA, startIndex, noOfPoints); viewPortDataSeriesB = (double[])Chart.arraySlice(dataSeriesB, startIndex, noOfPoints); chartTimeLimit = timeStamps[currentIndex - 1]; } // // At this stage, we have extracted the visible data. We can use those data to plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object of size 640 x 350 pixels XYChart c = new XYChart(512, 288); // Set the position, size and colors of the plot area c.setPlotArea(23, 33, c.getWidth() - 41, c.getHeight() - 53, c.linearGradientColor(0, 33, 0, c.getHeight() - 53, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // As the data can lie outside the plotarea in a zoomed chart, we need enable clipping. c.setClipping(); // Add a title to the chart using 18 pts Arial font c.addTitle("Gerçek Zamanlı Motor Verileri", "Arial", 18); // Add a legend box at (60, 28) using horizontal layout. Use 8pts Arial Bold as font. Set the // background and border color to Transparent and use line style legend key. LegendBox b = c.addLegend(60, 28, false, "Arial Bold", 10); b.setBackground(Chart.Transparent); 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 Bold", 10, 0x336699); // Set the y-axis tick length to 0 to disable the tick and put the labels closer to the axis. c.yAxis().setLabelGap(-1); c.yAxis().setLabelAlignment(1); c.yAxis().setTickLength(0); c.yAxis().setMargin(20); // Add axis title using 12pts Arial Bold Italic font c.yAxis().setTitle("Angular Rate", "Arial Bold", 12); // Configure the x-axis tick length to 1 to put the labels closer to the axis. c.xAxis().setTickLength(1); //================================================================================ // Add data to chart //================================================================================ // // In this example, we represent the data by lines. You may modify the code below to use other // representations (areas, scatter plot, etc). // // Add a line layer for the lines, using a line width of 2 pixels LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); layer.setFastLineMode(); // Now we add the 3 data series to a line layer, using the color red (ff0000), green (00cc00) // and blue (0000ff) layer.setXData(viewPortTimeStamps); layer.addDataSet(viewPortDataSeriesA, 0x00cc00, "Motor Devir"); layer.addDataSet(viewPortDataSeriesB, 0x0000ff, "Motor Sicaklik"); //================================================================================ // Configure axis scale and labelling //================================================================================ if (currentIndex > 0) { c.xAxis().setDateScale(viewPortStartDate, viewPortEndDate); } // For the automatic axis labels, set the minimum spacing to 75/30 pixels for the x/y axis. c.xAxis().setTickDensity(75); c.yAxis().setTickDensity(30); // We use "hh:nn:ss" as the axis label format. c.xAxis().setLabelFormat("{value|nn:ss}"); // We make sure the tick increment must be at least 1 second. c.xAxis().setMinTickInc(1); //================================================================================ // Output the chart //================================================================================ // We need to update the track line too. trackLineLabel(c); // Set the chart image to the WinChartViewer viewer.Chart = c; }
//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) { // In this example, we simply use random data for the 2 data series. RanSeries r = new RanSeries(127); double[] data0 = r.getSeries(180, 70, -5, 5); double[] data1 = r.getSeries(180, 150, -15, 15); DateTime[] timeStamps = r.getDateSeries(180, new DateTime(2014, 3, 1), 86400); // Create a XYChart object of size 640 x 420 pixels XYChart c = new XYChart(640, 420); // Set default text color to dark grey (0x333333) c.setColor(Chart.TextColor, 0x333333); // Add a title box using grey (0x555555) 20pt Arial Bold font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial Bold", 20, 0x555555); // Set the plotarea at (70, 70) and of size 540 x 320 pixels, with transparent // background and border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(70, 70, 540, 320, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box with horizontal layout above the plot area at (70, 32). Use 12pt // Arial Bold dark grey (0x555555) font, transparent background and border, and line // style legend icon. LegendBox b = c.addLegend(70, 32, false, "Arial Bold", 12); b.setFontColor(0x555555); b.setBackground(Chart.Transparent, Chart.Transparent); b.setLineStyleKey(); // Set axis label font to 12pt Arial c.xAxis().setLabelStyle("Arial", 12); c.yAxis().setLabelStyle("Arial", 12); // Set the x and y axis stems to transparent, and the x-axis tick color to grey // (0xaaaaaa) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xaaaaaa); c.yAxis().setColors(Chart.Transparent); // Set the major/minor tick lengths for the x-axis to 10 and 0. c.xAxis().setTickLength(10, 0); // For the automatic axis labels, set the minimum spacing to 80/40 pixels for the x/y // axis. c.xAxis().setTickDensity(80); c.yAxis().setTickDensity(40); // Use "mm/yyyy" as the x-axis label format for the first plotted month of a year, and // "mm" for other months c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "{value|mm/yyyy} ", Chart.StartOfMonthFilter(), "{value|mm}"); // Add a title to the y axis using dark grey (0x555555) 12pt Arial Bold font c.yAxis().setTitle("Energy (kWh)", "Arial Bold", 14, 0x555555); // Add a line layer with 2-pixel line width LineLayer layer0 = c.addLineLayer(data0, 0xcc0000, "Power Usage"); layer0.setXData(timeStamps); layer0.setLineWidth(2); // Add an area layer using semi-transparent blue (0x7f0044cc) as the fill color AreaLayer layer1 = c.addAreaLayer(data1, 0x7f0044cc, "Effective Load"); layer1.setXData(timeStamps); layer1.setBorderColor(Chart.SameAsMainColor); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='[{x|mm dd, yyyy}] {value} kWh'"); }
/// <summary> /// 绘制噪声数据比较分析图 /// </summary> private void CreateChart(int line) { if (sRecorder == null) { return; } XYChart c = new XYChart(700, 360); c.setBackground(c.linearGradientColor(0, 0, 0, 100, 0x99ccff, 0xffffff), 0x888888); ChartDirector.TextBox title = c.addTitle("噪声数据比较分析图", "Arial Bold", 13); title.setPos(0, 20); c.setPlotArea(80, 80, 580, 230, 0xffffff, -1, -1, c.dashLineColor( 0xaaaaaa, Chart.DotLine), -1); LegendBox legendBox = c.addLegend(350, 80, false, "Arial", 8); legendBox.setAlignment(Chart.BottomCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); legendBox.setLineStyleKey(); legendBox.setFontSize(8); c.xAxis().setIndent(true); c.xAxis().setTitle("噪声频率(Hz)"); c.yAxis().setTitle("噪声幅度(%)"); LineLayer layer1; ChartDirector.DataSet ds; double[] dataSet; double[] da; dataSet = sRecorder.Data.Amplitude.Skip(4).ToArray(); da = sRecorder.Data.Frequency.Skip(4).ToArray(); switch (line) { case 0: layer1 = c.addLineLayer(); ds = layer1.addDataSet(dataSet, GetRandomColor(0), "记录仪" + sRecorder.ID); layer1.setLineWidth(2); layer1.setXData(da); break; case 1: layer1 = c.addSplineLayer(); ds = layer1.addDataSet(dataSet, GetRandomColor(0), "记录仪" + sRecorder.ID); layer1.setLineWidth(2); layer1.setXData(da); break; } if (eRecorder != null && sRecorder.ID != eRecorder.ID) { dataSet = eRecorder.Data.Amplitude.Skip(4).ToArray(); da = eRecorder.Data.Frequency.Skip(4).ToArray(); switch (line) { case 0: layer1 = c.addLineLayer(); ds = layer1.addDataSet(dataSet, GetRandomColor(1), "记录仪" + eRecorder.ID); layer1.setLineWidth(2); layer1.setXData(da); break; case 1: layer1 = c.addSplineLayer(); ds = layer1.addDataSet(dataSet, GetRandomColor(1), "记录仪" + eRecorder.ID); layer1.setLineWidth(2); layer1.setXData(da); break; } } c.xAxis().setLabelStep(15); c.yAxis().setDateScale(0, 120); winChartViewer1.Chart = c; winChartViewer1.ImageMap = c.getHTMLImageMap("clickable", "", "title='噪声频率: {x}Hz, \n{dataSetName}: {value}%'"); }
//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 bars and the marks double[] barData = { 100, 125, 245, 147, 67, 96, 160, 145, 97, 167, 220, 125 }; double[] markData = { 85, 156, 220, 120, 80, 110, 140, 130, 111, 180, 175, 100 }; // The labels for the bar chart string[] labels = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; // Create a XYChart object of size 480 x 360 pixels. Use a vertical gradient color from // pale blue (e8f0f8) to sky blue (aaccff) spanning half the chart height as background. // Set border to blue (88aaee). Use rounded corners. Enable soft drop shadow. XYChart c = new XYChart(480, 360); c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xe8f0f8, 0xaaccff), 0x88aaee); c.setRoundedFrame(); c.setDropShadow(); // Add a title to the chart using 15 points Arial Italic font. Set top/bottom margins to // 12 pixels. ChartDirector.TextBox title = c.addTitle("Bars with Marks Demonstration", "Arial Italic", 15); title.setMargin2(0, 0, 12, 12); // Tentatively set the plotarea to 50 pixels from the left edge to allow for the y-axis, // and to just under the title. Set the width to 65 pixels less than the chart width, // and the height to reserve 90 pixels at the bottom for the x-axis and the legend box. // Use pale blue (e8f0f8) background, transparent border, and grey (888888) dotted // horizontal grid lines. c.setPlotArea(50, title.getHeight(), c.getWidth() - 65, c.getHeight() - title.getHeight( ) - 90, 0xe8f0f8, -1, Chart.Transparent, c.dashLineColor(0x888888, Chart.DotLine)); // Add a legend box where the bottom-center is anchored to the 15 pixels above the // bottom-center of the chart. Use horizontal layout and 8 points Arial font. LegendBox legendBox = c.addLegend(c.getWidth() / 2, c.getHeight() - 15, false, "Arial", 8); legendBox.setAlignment(Chart.BottomCenter); // Set the legend box background and border to pale blue (e8f0f8) and bluish grey // (445566) legendBox.setBackground(0xe8f0f8, 0x445566); // Use rounded corners of 5 pixel radius for the legend box legendBox.setRoundedCorners(5); // Use line style legend key legendBox.setLineStyleKey(); // Set axes to transparent c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); // Set the labels on the x axis c.xAxis().setLabels(labels); // Add a box-whisker layer with just the middle mark visible for the marks. Use red // (ff0000) color for the mark, with a line width of 2 pixels and 10% horizontal gap BoxWhiskerLayer markLayer = c.addBoxWhiskerLayer(null, null, null, null, markData, -1, 0xff0000); markLayer.setLineWidth(2); markLayer.setDataGap(0.1); // Add the legend key for the mark line legendBox.addKey("Target", 0xff0000, 2); // Tool tip for the mark layer markLayer.setHTMLImageMap("", "", "title='Target at {xLabel}: {med}'"); // Add a blue (0066cc) bar layer using the given data. BarLayer barLayer = c.addBarLayer(barData, 0x0066cc, "Actual"); // Use soft lighting effect for the bars with light direction from left. barLayer.setBorderColor(Chart.Transparent, Chart.softLighting(Chart.Left)); // Tool tip for the bar layer barLayer.setHTMLImageMap("", "", "title='{dataSetName} at {xLabel}: {value}'"); // Adjust the plot area size, such that the bounding box (inclusive of axes) is 10 // pixels from the left edge, just below the title, 15 pixels from the right edge, and // 10 pixels above the legend box. c.packPlotArea(10, title.getHeight(), c.getWidth() - 15, c.layoutLegend().getTopY() - 10 ); // Output the chart viewer.Chart = c; // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable"); }
// // Draw the chart. // private void drawChart(WPFChartViewer viewer) { // Get the start date and end date that are visible on the chart. DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft)); DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth)); // Extract the part of the data arrays that are visible. DateTime[] viewPortTimeStamps = null; double[] viewPortDataSeriesA = null; double[] viewPortDataSeriesB = null; double[] viewPortDataSeriesC = null; if (currentIndex > 0) { // Get the array indexes that corresponds to the visible start and end dates int startIndex = (int)Math.Floor(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortStartDate)); int endIndex = (int)Math.Ceiling(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortEndDate)); int noOfPoints = endIndex - startIndex + 1; // Extract the visible data viewPortTimeStamps = (DateTime[])Chart.arraySlice(timeStamps, startIndex, noOfPoints); viewPortDataSeriesA = (double[])Chart.arraySlice(dataSeriesA, startIndex, noOfPoints); viewPortDataSeriesB = (double[])Chart.arraySlice(dataSeriesB, startIndex, noOfPoints); viewPortDataSeriesC = (double[])Chart.arraySlice(dataSeriesC, startIndex, noOfPoints); } // // At this stage, we have extracted the visible data. We can use those data to plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object of size 640 x 350 pixels XYChart c = new XYChart(640, 350); // Set the plotarea at (55, 50) with width 80 pixels less than chart width, and height 85 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(55, 50, c.getWidth() - 85, c.getHeight() - 80, c.linearGradientColor(0, 50, 0, c.getHeight() - 35, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // As the data can lie outside the plotarea in a zoomed chart, we need enable clipping. c.setClipping(); // Add a title to the chart using 18 pts Times New Roman Bold Italic font c.addTitle(" Realtime Chart with Zoom/Scroll and Track Line", "Arial", 18); // Add a legend box at (55, 25) using horizontal layout. Use 8pts Arial Bold as font. Set the // background and border color to Transparent and use line style legend key. LegendBox b = c.addLegend(55, 25, false, "Arial Bold", 10); b.setBackground(Chart.Transparent); 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); // Add axis title using 10pts Arial Bold Italic font c.yAxis().setTitle("Ionic Temperature (C)", "Arial Bold", 10); //================================================================================ // Add data to chart //================================================================================ // // In this example, we represent the data by lines. You may modify the code below to use other // representations (areas, scatter plot, etc). // // Add a line layer for the lines, using a line width of 2 pixels LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); layer.setFastLineMode(); // Now we add the 3 data series to a line layer, using the color red (ff0000), green (00cc00) // and blue (0000ff) layer.setXData(viewPortTimeStamps); layer.addDataSet(viewPortDataSeriesA, 0xff0000, "Alpha"); layer.addDataSet(viewPortDataSeriesB, 0x00cc00, "Beta"); layer.addDataSet(viewPortDataSeriesC, 0x0000ff, "Gamma"); //================================================================================ // Configure axis scale and labelling //================================================================================ if (currentIndex > 0) { c.xAxis().setDateScale(viewPortStartDate, viewPortEndDate); } // For the automatic axis labels, set the minimum spacing to 75/30 pixels for the x/y axis. c.xAxis().setTickDensity(75); c.yAxis().setTickDensity(30); // // In a zoomable chart, the time range can be from a few years to a few seconds. We can need // to define the date/time format the various cases. // // If all ticks are year aligned, we use "yyyy" as the label format. c.xAxis().setFormatCondition("align", 360 * 86400); c.xAxis().setLabelFormat("{value|yyyy}"); // If all ticks are month aligned, we use "mmm yyyy" in bold font as the first label of a year, // and "mmm" for other labels. c.xAxis().setFormatCondition("align", 30 * 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*font=bold*>{value|mmm yyyy}", Chart.AllPassFilter(), "{value|mmm}"); // If all ticks are day algined, we use "mmm dd<*br*>yyyy" in bold font as the first label of a // year, and "mmm dd" in bold font as the first label of a month, and "dd" for other labels. c.xAxis().setFormatCondition("align", 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*block,halign=left*><*font=bold*>{value|mmm dd<*br*>yyyy}", Chart.StartOfMonthFilter(), "<*font=bold*>{value|mmm dd}"); c.xAxis().setMultiFormat2(Chart.AllPassFilter(), "{value|dd}"); // If all ticks are hour algined, we use "hh:nn<*br*>mmm dd" in bold font as the first label of // the Day, and "hh:nn" for other labels. c.xAxis().setFormatCondition("align", 3600); c.xAxis().setMultiFormat(Chart.StartOfDayFilter(), "<*font=bold*>{value|hh:nn<*br*>mmm dd}", Chart.AllPassFilter(), "{value|hh:nn}"); // If all ticks are minute algined, then we use "hh:nn" as the label format. c.xAxis().setFormatCondition("align", 60); c.xAxis().setLabelFormat("{value|hh:nn}"); // If all other cases, we use "hh:nn:ss" as the label format. c.xAxis().setFormatCondition("else"); c.xAxis().setLabelFormat("{value|hh:nn:ss}"); // We make sure the tick increment must be at least 1 second. c.xAxis().setMinTickInc(1); //================================================================================ // Output the chart //================================================================================ // We need to update the track line too. If the mouse is moving on the chart (eg. if // the user drags the mouse on the chart to scroll it), the track line will be updated // in the MouseMovePlotArea event. Otherwise, we need to update the track line here. if (!viewer.IsInMouseMoveEvent) { trackLineLabel(c, (null == viewer.Chart) ? c.getPlotArea().getRightX() : viewer.PlotAreaMouseX); } viewer.Chart = c; }
// // 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; }
// // Draw the chart. // private void drawChart(WPFChartViewer viewer) { // Get the start date and end date that are visible on the chart. double viewPortStartDate = viewer.getValueAtViewPort("x", viewer.ViewPortLeft); double viewPortEndDate = viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth); // Extract the part of the data arrays that are visible. double[] viewPortTimeStamps = null; double[] viewPortDataSeriesA = null; double[] viewPortDataSeriesB = null; if (currentIndex > 0) { // Get the array indexes that corresponds to the visible start and end dates int startIndex = (int)Math.Floor(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortStartDate)); int endIndex = (int)Math.Ceiling(Chart.bSearch2(timeStamps, 0, currentIndex, viewPortEndDate)); int noOfPoints = endIndex - startIndex + 1; // Extract the visible data viewPortTimeStamps = (double[])Chart.arraySlice(timeStamps, startIndex, noOfPoints); viewPortDataSeriesA = (double[])Chart.arraySlice(dataSeriesA, startIndex, noOfPoints); viewPortDataSeriesB = (double[])Chart.arraySlice(dataSeriesB, startIndex, noOfPoints); // Keep track of the latest available data at chart plotting time trackLineEndPos = timeStamps[currentIndex - 1]; } // // At this stage, we have extracted the visible data. We can use those data to plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object of size 640 x 350 pixels XYChart c = new XYChart(640, 350); // Set the plotarea at (55, 50) with width 85 pixels less than chart width, and height 80 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(55, 50, c.getWidth() - 85, c.getHeight() - 80, c.linearGradientColor(0, 50, 0, c.getHeight() - 30, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // As the data can lie outside the plotarea in a zoomed chart, we need enable clipping. c.setClipping(); // Add a title to the chart using 18 pts Times New Roman Bold Italic font c.addTitle(" Multithreading Real-Time Chart", "Arial", 18); // Add a legend box at (55, 25) using horizontal layout. Use 8pts Arial Bold as font. Set the // background and border color to Transparent and use line style legend key. LegendBox b = c.addLegend(55, 25, false, "Arial Bold", 10); b.setBackground(Chart.Transparent); 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); // Add axis title using 10pts Arial Bold Italic font c.yAxis().setTitle("Ionic Temperature (C)", "Arial Bold", 10); //================================================================================ // Add data to chart //================================================================================ // // In this example, we represent the data by lines. You may modify the code below to use other // representations (areas, scatter plot, etc). // // Add a line layer for the lines, using a line width of 2 pixels LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); layer.setFastLineMode(); // Now we add the 3 data series to a line layer, using the color red (ff0000), green (00cc00) // and blue (0000ff) layer.setXData(viewPortTimeStamps); layer.addDataSet(viewPortDataSeriesA, 0xff0000, "Alpha"); layer.addDataSet(viewPortDataSeriesB, 0x00cc00, "Beta"); //================================================================================ // Configure axis scale and labelling //================================================================================ if (currentIndex > 0) { c.xAxis().setDateScale(viewPortStartDate, viewPortEndDate); } // For the automatic axis labels, set the minimum spacing to 75/30 pixels for the x/y axis. c.xAxis().setTickDensity(75); c.yAxis().setTickDensity(30); // We use "hh:nn:ss" as the axis label format. c.xAxis().setLabelFormat("{value|hh:nn:ss}"); // We make sure the tick increment must be at least 1 second. c.xAxis().setMinTickInc(1); // 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 //================================================================================ // We need to update the track line too. If the mouse is moving on the chart (eg. if // the user drags the mouse on the chart to scroll it), the track line will be updated // in the MouseMovePlotArea event. Otherwise, we need to update the track line here. if (!WPFChartViewer1.IsInMouseMoveEvent) { trackLineLabel(c, trackLineIsAtEnd ? c.getWidth() : viewer.PlotAreaMouseX); } viewer.Chart = c; }
private void Window_Loaded(object sender, RoutedEventArgs e) { // The XY data of the first data series double[] dataX = { 50, 55, 37, 24, 42, 49, 63, 72, 83, 59 }; double[] dataY = { 3.6, 2.8, 2.5, 2.3, 3.8, 3.0, 3.8, 5.0, 6.0, 3.3 }; // Create a XYChart object of size 520 x 490 pixels XYChart c = new XYChart(520, 490); // Set the plotarea at (60, 40) and of size 450 x 400 pixels, with white background and a light // grey border (0xc0c0c0). Turn on both horizontal and vertical grid lines with light grey color // (0xc0c0c0) c.setPlotArea(60, 40, 450, 400, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a title to the chart using 18 point Times Bold Itatic font. c.addTitle(" Chemical X Thermal Conductivity", "Times New Roman Bold Italic", 18); // Add titles to the axes using 12pt Arial Bold Italic font c.yAxis().setTitle("Thermal Conductivity (W/K)", "Arial Bold Italic", 12); c.xAxis().setTitle("Concentration (g/liter)", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.yAxis().setWidth(3); c.xAxis().setWidth(3); // Add a scatter layer using (dataX, dataY) ScatterLayer scatterLayer = c.addScatterLayer(dataX, dataY, "", Chart.GlassSphereShape, 13, 0xcc0000); // Tooltip for the scatter layer scatterLayer.setHTMLImageMap("", "", "title='Concentration: {x} g/liter, Conductivity: {value} W/K'"); // Add a trend line layer for (dataX, dataY) TrendLayer trendLayer = c.addTrendLayer2(dataX, dataY, 0xcc0000); // Set the line width to 3 pixels trendLayer.setLineWidth(3); // Add a 95% confidence band for the line trendLayer.addConfidenceBand(0.95, unchecked ((int)0x806666ff)); // Add a 95% confidence band (prediction band) for the points trendLayer.addPredictionBand(0.95, unchecked ((int)0x8066ff66)); // Tool tip for the trend layer trendLayer.setHTMLImageMap("", "", "title='Trend Line: y = {slope|P4} x + {intercept|P4}'"); // Add a legend box at (60, 35) (top of the chart) with horizontal layout. Use 10pt Arial Bold // Italic font. Set the background and border color to Transparent and use line style legend // icons. LegendBox legendBox = c.addLegend(60, 35, false, "Arial Bold Italic", 9); legendBox.setBackground(Chart.Transparent); legendBox.setLineStyleKey(true); // Add entries to the legend box legendBox.addKey("95% Line Confidence", unchecked ((int)0x806666ff)); legendBox.addKey("95% Point Confidence", unchecked ((int)0x8066ff66)); legendBox.addKey(String.Format("Trend Line: y = {0:0.0000} x + {1:0.0000}", trendLayer.getSlope(), trendLayer.getIntercept()), 0xcc0000, 3); // Assign the chart to the WPFChartViewer WPFChartViewer1.Chart = c; // Include tool tip for the chart WPFChartViewer1.ImageMap = c.getHTMLImageMap(""); }
//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 line chart double[] data0 = { 70, 73, 80, 90, 95, 93, 82, 77, 82, 101, 111, 115 }; double[] data1 = { 90, 96, 89, 77, 82, 96, 109, 109, 99, 108, 96, 91 }; double[] data2 = { 58, 34, 25, 49, 64, 10, 16, 40, 25, 49, 40, 22 }; // The labels for the line chart string[] labels = { "2008-01", "2008-02", "2008-03", "2008-04", "2008-05", "2008-06", "2008-07", "2008-08", "2008-09", "2008-10", "2008-11", "2008-12" }; // Create a XYChart object of size 450 x 450 pixels XYChart c = new XYChart(450, 450); // Add a title to the chart using 15pt Arial Italic font. ChartDirector.TextBox title = c.addTitle("Inter-line Coloring", "Arial Italic", 15); // Add a legend box where the top-center is anchored to the horizontal center of the // chart, just under the title. Use horizontal layout and 10 points Arial Bold font, and // transparent background and border. Use line style legend key. LegendBox legendBox = c.addLegend(c.getWidth() / 2, title.getHeight(), false, "Arial Bold Italic", 10); legendBox.setBackground(Chart.Transparent, Chart.Transparent); legendBox.setAlignment(Chart.TopCenter); legendBox.setLineStyleKey(); // Tentatively set the plotarea at (70, 65) and of (chart_width - 100) x (chart_height - // 110) in size. Use light grey (c0c0c0) border and horizontal and vertical grid lines. PlotArea plotArea = c.setPlotArea(70, 65, c.getWidth() - 100, c.getHeight() - 110, -1, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a title to the y axis using 12pt Arial Bold Italic font c.yAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); // Add a title to the x axis using 12pt Arial Bold Italic font c.xAxis().setTitle("Axis Title Placeholder", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.xAxis().setWidth(3); c.yAxis().setWidth(3); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Use 8 points Arial rotated by 90 degrees as the x-axis label font c.xAxis().setLabelStyle("Arial", 8, Chart.TextColor, 90); // Add a spline curve to the chart SplineLayer layer0 = c.addSplineLayer(data0, 0xff0000, "Data Set 0"); layer0.setLineWidth(2); // Add a normal line to the chart LineLayer layer1 = c.addLineLayer(data1, 0x008800, "Data Set 1"); layer1.setLineWidth(2); // Color the region between the above spline curve and normal line. Use the // semi-transparent red (80ff000000) if the spline curve is higher than the normal line, // otherwise use semi-transparent green (80008800) c.addInterLineLayer(layer0.getLine(), layer1.getLine(), unchecked ((int)0x80ff0000), unchecked ((int)0x80008800)); // Add another normal line to the chart LineLayer layer2 = c.addLineLayer(data2, 0x0000ff, "Data Set 2"); layer2.setLineWidth(2); // Add a horizontal mark line to the chart at y = 40 Mark mark = c.yAxis().addMark(40, -1, "Threshold"); mark.setLineWidth(2); // Set the mark line to purple (880088) dash line. Use white (ffffff) for the mark // label. mark.setMarkColor(c.dashLineColor(0x880088), 0xffffff); // Put the mark label at the left side of the mark, with a purple (880088) background. mark.setAlignment(Chart.Left); mark.setBackground(0x880088); // Color the region between the above normal line and mark line. Use the // semi-transparent blue (800000ff) if the normal line is higher than the mark line, // otherwise use semi-transparent purple (80880088) c.addInterLineLayer(layer2.getLine(), mark.getLine(), unchecked ((int)0x800000ff), unchecked ((int)0x80880088)); // Layout the legend box, so we can get its height c.layoutLegend(); // Adjust the plot area size, such that the bounding box (inclusive of axes) is 10 // pixels from the left edge, just under the legend box, 25 pixels from the right edge, // and 10 pixels from the bottom edge. c.packPlotArea(10, legendBox.getTopY() + legendBox.getHeight(), c.getWidth() - 25, c.getHeight() - 10); // After determining the exact plot area position, we may adjust the legend box and the // title positions so that they are centered relative to the plot area (instead of the // chart) legendBox.setPos(plotArea.getLeftX() + (plotArea.getWidth() - legendBox.getWidth()) / 2, legendBox.getTopY()); title.setPos(plotArea.getLeftX() + (plotArea.getWidth() - title.getWidth()) / 2, title.getTopY()); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='{dataSetName} in {xLabel}: {value}'"); }
// // Draw the chart // private void drawChart(RazorChartViewer viewer) { // // Data to draw the chart. In this demo, the data buffer will be filled by a random data // generator. In real life, the data is probably stored in a buffer (eg. a database table, a // text file, or some global memory) and updated by other means. // // We use a data buffer to emulate the last 240 samples. int sampleSize = 240; double[] dataSeries1 = new double[sampleSize]; double[] dataSeries2 = new double[sampleSize]; double[] dataSeries3 = new double[sampleSize]; DateTime[] timeStamps = new DateTime[sampleSize]; // Our pseudo random number generator DateTime firstDate = DateTime.Now.AddSeconds(-timeStamps.Length); for (int i = 0; i < timeStamps.Length; ++i) { timeStamps[i] = firstDate.AddSeconds(i); double p = timeStamps[i].Ticks / 10000000; dataSeries1[i] = Math.Cos(p * 2.1) * 10 + 1 / (Math.Cos(p) * Math.Cos(p) + 0.01) + 20; dataSeries2[i] = 100 * Math.Sin(p / 27.7) * Math.Sin(p / 10.1) + 150; dataSeries3[i] = 100 * Math.Cos(p / 6.7) * Math.Cos(p / 11.9) + 150; } // Create an XYChart object 600 x 320 pixels in size XYChart c = new XYChart(600, 320); // Set the plotarea at (55, 60) and of size 520 x 235 pixels with transparent background and // border. Enable both horizontal and vertical grids by setting their colors to grey // (cccccc). Set clipping mode to clip the data lines to the plot area. c.setPlotArea(55, 60, 520, 235, -1, -1, Chart.Transparent, 0xcccccc, 0xcccccc); c.setClipping(); // Add a title to the chart using dark grey (0x333333) 20pt Arial Bold font c.addTitle("Realtime Chart with Snapshot", "Arial Bold", 20, 0x333333); // Add a legend box at the top of the plot area using horizontal layout. Use 10pt Arial Bold // font, transparent background and border, and line style legend icon. LegendBox b = c.addLegend(55, 30, false, "Arial Bold", 10); b.setBackground(Chart.Transparent, Chart.Transparent); 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); // Add y-axis title using 12pt Arial font c.yAxis().setTitle("Y-Axis Title Placeholder", "Arial", 12); // For the automatic x and y axis labels, set the minimum spacing to 75 and 30 pixels. c.xAxis().setTickDensity(75); c.yAxis().setTickDensity(30); // Set the x-axis label format c.xAxis().setLabelFormat("{value|hh:nn:ss}"); // Create a line layer to plot the lines LineLayer layer = c.addLineLayer2(); // The x-coordinates are the timeStamps. layer.setXData(timeStamps); // The 3 data series are used to draw 3 lines. Here we put the latest data values as part of // the data set name, so you can see them updated in the legend box. layer.addDataSet(dataSeries1, 0xff0000, c.formatValue(dataSeries1[dataSeries1.Length - 1], "Alpha: {value|2}")); layer.addDataSet(dataSeries2, 0x00cc00, c.formatValue(dataSeries2[dataSeries2.Length - 1], "Beta: {value|2}")); layer.addDataSet(dataSeries3, 0x0000ff, c.formatValue(dataSeries3[dataSeries3.Length - 1], "Gamma: {value|2}")); // Determine the output format if (Request["download"] == "pdf") { viewer.Image = c.makeWebImage(Chart.PDF); } else { viewer.Image = c.makeWebImage(Chart.PNG); } }
// // Draw the chart. // private void drawChart(WPFChartViewer viewer) { // Get the start date and end date that are visible on the chart. DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft)); DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth)); // Get the array indexes that corresponds to the visible start and end dates int startIndex = (int)Math.Floor(Chart.bSearch(timeStamps, viewPortStartDate)); int endIndex = (int)Math.Ceiling(Chart.bSearch(timeStamps, viewPortEndDate)); int noOfPoints = endIndex - startIndex + 1; // Extract the part of the data array that are visible. DateTime[] viewPortTimeStamps = (DateTime[])Chart.arraySlice(timeStamps, startIndex, noOfPoints); double[] viewPortDataSeriesA = (double[])Chart.arraySlice(dataSeriesA, startIndex, noOfPoints); double[] viewPortDataSeriesB = (double[])Chart.arraySlice(dataSeriesB, startIndex, noOfPoints); double[] viewPortDataSeriesC = (double[])Chart.arraySlice(dataSeriesC, startIndex, noOfPoints); // // At this stage, we have extracted the visible data. We can use those data to plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object of size 640 x 350 pixels XYChart c = new XYChart(640, 350); // Set the plotarea at (55, 50) with width 80 pixels less than chart width, and height 85 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(55, 50, c.getWidth() - 80, c.getHeight() - 85, c.linearGradientColor(0, 50, 0, c.getHeight() - 35, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // As the data can lie outside the plotarea in a zoomed chart, we need enable clipping. c.setClipping(); // Add a title to the chart using 18 pts Times New Roman Bold Italic font c.addTitle(" Zooming and Scroll with Track Line (2)", "Times New Roman Bold Italic", 18); // Add a legend box at (55, 25) using horizontal layout. Use 8pts Arial Bold as font. Set the // background and border color to Transparent and use line style legend key. LegendBox b = c.addLegend(55, 25, false, "Arial Bold", 8); b.setBackground(Chart.Transparent); b.setLineStyleKey(); // Set the axis stem to transparent c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); // Add axis title using 10pts Arial Bold Italic font c.yAxis().setTitle("Ionic Temperature (C)", "Arial Bold Italic", 10); //================================================================================ // Add data to chart //================================================================================ // // In this example, we represent the data by lines. You may modify the code below to use other // representations (areas, scatter plot, etc). // // Add a line layer for the lines, using a line width of 2 pixels LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); // In this demo, we do not have too many data points. In real code, the chart may contain a lot // of data points when fully zoomed out - much more than the number of horizontal pixels in this // plot area. So it is a good idea to use fast line mode. layer.setFastLineMode(); // Now we add the 3 data series to a line layer, using the color red (ff33333), green (008800) // and blue (3333cc) layer.setXData(viewPortTimeStamps); layer.addDataSet(viewPortDataSeriesA, 0xff3333, "Alpha"); layer.addDataSet(viewPortDataSeriesB, 0x008800, "Beta"); layer.addDataSet(viewPortDataSeriesC, 0x3333cc, "Gamma"); //================================================================================ // Configure axis scale and labelling //================================================================================ // Set the x-axis as a date/time axis with the scale according to the view port x range. viewer.syncDateAxisWithViewPort("x", c.xAxis()); // // In this demo, the time range can be from a few years to a few days. We demonstrate how to set // up different date/time format based on the time range. // // If all ticks are yearly aligned, then we use "yyyy" as the label format. c.xAxis().setFormatCondition("align", 360 * 86400); c.xAxis().setLabelFormat("{value|yyyy}"); // If all ticks are monthly aligned, then we use "mmm yyyy" in bold font as the first label of a // year, and "mmm" for other labels. c.xAxis().setFormatCondition("align", 30 * 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*font=bold*>{value|mmm yyyy}", Chart.AllPassFilter(), "{value|mmm}"); // If all ticks are daily algined, then we use "mmm dd<*br*>yyyy" in bold font as the first // label of a year, and "mmm dd" in bold font as the first label of a month, and "dd" for other // labels. c.xAxis().setFormatCondition("align", 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*block,halign=left*><*font=bold*>{value|mmm dd<*br*>yyyy}", Chart.StartOfMonthFilter(), "<*font=bold*>{value|mmm dd}"); c.xAxis().setMultiFormat2(Chart.AllPassFilter(), "{value|dd}"); // For all other cases (sub-daily ticks), use "hh:nn<*br*>mmm dd" for the first label of a day, // and "hh:nn" for other labels. c.xAxis().setFormatCondition("else"); c.xAxis().setMultiFormat(Chart.StartOfDayFilter(), "<*font=bold*>{value|hh:nn<*br*>mmm dd}", Chart.AllPassFilter(), "{value|hh:nn}"); //================================================================================ // Output the chart //================================================================================ // We need to update the track line too. If the mouse is moving on the chart (eg. if // the user drags the mouse on the chart to scroll it), the track line will be updated // in the MouseMovePlotArea event. Otherwise, we need to update the track line here. if ((!viewer.IsInMouseMoveEvent) && viewer.IsMouseOnPlotArea) { trackLineLabel(c, viewer.PlotAreaMouseX); } viewer.Chart = c; }
// // Draw the chart // private void drawChart(RazorChartViewer viewer) { // Determine the visible x-axis range DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft)) ; DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth)); // We need to get the data within the visible x-axis range. In real code, this can be by // using a database query or some other means as specific to the application. In this demo, // we just generate a random data table, and then select the data within the table. RanTable r = getRandomTable(); // Select the data for the visible date range viewPortStartDate to viewPortEndDate. It is // possible there is no data point at exactly viewPortStartDate or viewPortEndDate. In this // case, we also need the data points that are just outside the visible date range to // "overdraw" the line a little bit (the "overdrawn" part will be clipped to the plot area) // In this demo, we do this by adding a one day margin to the date range when selecting the // data. r.selectDate(0, viewPortStartDate.AddDays(-1), viewPortEndDate.AddDays(1)); // The selected data from the random data table DateTime[] timeStamps = Chart.NTime(r.getCol(0)); double[] dataSeriesA = r.getCol(1); double[] dataSeriesB = r.getCol(2); double[] dataSeriesC = r.getCol(3); // // Now we have obtained the data, we can plot the chart. // //================================================================================ // Configure overall chart appearance. //================================================================================ // Create an XYChart object of size 640 x 400 pixels XYChart c = new XYChart(640, 400); // Set the plotarea at (55, 55) with width 80 pixels less than chart width, and height 90 // 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(55, 55, c.getWidth() - 80, c.getHeight() - 90, c.linearGradientColor(0, 55, 0, c.getHeight() - 35, 0xf0f6ff, 0xa0c0ff), -1, Chart.Transparent, 0xffffff, 0xffffff); // As the data can lie outside the plotarea in a zoomed chart, we need to enable clipping. c.setClipping(); // Add a title box using dark grey (0x333333) 18pt Arial Bold font c.addTitle(" Zooming and Scrolling with Viewport Control", "Arial Bold", 15, 0x333333); if (viewer.IsAttachmentRequest()) { LegendBox b = c.addLegend(55, 28, false, "Arial Bold", 10); b.setBackground(Chart.Transparent, Chart.Transparent); 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); // Add axis title using 10pt Arial Bold font c.yAxis().setTitle("Ionic Temperature (C)", "Arial Bold", 10); //================================================================================ // Add data to chart //================================================================================ // // In this example, we represent the data by lines. You may modify the code below to use // other layer types (areas, scatter plot, etc). // // Add a line layer for the lines, using a line width of 2 pixels LineLayer layer = c.addLineLayer2(); layer.setLineWidth(2); // In this demo, we do not have too many data points. In real code, the chart may contain a // lot of data points when fully zoomed out - much more than the number of horizontal pixels // in this plot area. So it is a good idea to use fast line mode. layer.setFastLineMode(); // Now we add the 3 data series to a line layer, using the color red (0xff3333), green // (0x008800) and blue (0x3333cc) layer.setXData(timeStamps); layer.addDataSet(dataSeriesA, 0xff3333, "Alpha"); layer.addDataSet(dataSeriesB, 0x008800, "Beta"); layer.addDataSet(dataSeriesC, 0x3333cc, "Gamma"); //================================================================================ // Configure axis scale and labelling //================================================================================ // Set the x-axis as a date/time axis with the scale according to the view port x range. viewer.syncDateAxisWithViewPort("x", c.xAxis()); // For the automatic y-axis labels, set the minimum spacing to 30 pixels. c.yAxis().setTickDensity(30); // // In this demo, the time range can be from a few years to a few days. We demonstrate how to // set up different date/time format based on the time range. // // If all ticks are yearly aligned, then we use "yyyy" as the label format. c.xAxis().setFormatCondition("align", 360 * 86400); c.xAxis().setLabelFormat("{value|yyyy}"); // If all ticks are monthly aligned, then we use "mmm yyyy" in bold font as the first label // of a year, and "mmm" for other labels. c.xAxis().setFormatCondition("align", 30 * 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*font=bold*>{value|mmm<*br*>yyyy}", Chart.AllPassFilter(), "{value|mmm}"); // If all ticks are daily algined, then we use "mmm dd<*br*>yyyy" in bold font as the first // label of a year, and "mmm dd" in bold font as the first label of a month, and "dd" for // other labels. c.xAxis().setFormatCondition("align", 86400); c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*block,halign=left*><*font=bold*>{value|mmm dd<*br*>yyyy}", Chart.StartOfMonthFilter(), "<*font=bold*>{value|mmm dd}"); c.xAxis().setMultiFormat2(Chart.AllPassFilter(), "{value|dd}"); // For all other cases (sub-daily ticks), use "hh:nn<*br*>mmm dd" for the first label of a // day, and "hh:nn" for other labels. c.xAxis().setFormatCondition("else"); c.xAxis().setMultiFormat(Chart.StartOfDayFilter(), "<*font=bold*>{value|hh:nn<*br*>mmm dd}", Chart.AllPassFilter(), "{value|hh:nn}"); //================================================================================ // Step 5 - Output the chart //================================================================================ if (viewer.IsAttachmentRequest()) { // Output as PDF attachment viewer.Image = c.makeWebImage(Chart.PDF); } else { // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Output Javascript chart model to the browser to suppport tracking cursor viewer.ChartModel = c.getJsChartModel(); } }