//Main code for creating chart. //Note: the argument img is unused because this demo only has 1 chart. public void createChart(WinChartViewer viewer, string img) { // The XY points for the scatter chart double[] dataX = { 150, 400, 300, 1500, 800 }; double[] dataY = { 0.6, 8, 5.4, 2, 4 }; // The labels for the points string[] labels = { "Nano\n100", "SpeedTron\n200 Lite", "SpeedTron\n200", "Marathon\nExtra", "Marathon\n2000" }; // Create a XYChart object of size 450 x 400 pixels XYChart c = new XYChart(450, 400); // Set the plotarea at (55, 40) and of size 350 x 300 pixels, with a // light grey border (0xc0c0c0). Turn on both horizontal and vertical // grid lines with light grey color (0xc0c0c0) c.setPlotArea(55, 40, 350, 300, 0xffffff, -1, 0xc0c0c0, 0xc0c0c0, -1); // Add a title to the chart using 18 pts Times Bold Itatic font. c.addTitle("Product Comparison Chart", "Times New Roman Bold Italic", 18) ; // Add a title to the y axis using 12 pts Arial Bold Italic font c.yAxis().setTitle("Capacity (tons)", "Arial Bold Italic", 12); // Add a title to the x axis using 12 pts Arial Bold Italic font c.xAxis().setTitle("Range (miles)", "Arial Bold Italic", 12); // Set the axes line width to 3 pixels c.xAxis().setWidth(3); c.yAxis().setWidth(3); // Add the data as a scatter chart layer, using a 15 pixel circle as the // symbol ScatterLayer layer = c.addScatterLayer(dataX, dataY, "", Chart.GlassSphereShape, 15, 0xff3333, 0xff3333); // Add labels to the chart as an extra field layer.addExtraField(labels); // Set the data label format to display the extra field layer.setDataLabelFormat("{field0}"); // Use 8pts Arial Bold to display the labels ChartDirector.TextBox textbox = layer.setDataLabelStyle("Arial Bold", 8); // Set the background to purple with a 1 pixel 3D border textbox.setBackground(0xcc99ff, Chart.Transparent, 1); // Put the text box 4 pixels to the right of the data point textbox.setAlignment(Chart.Left); textbox.setPos(4, 0); // Output the chart viewer.Image = c.makeImage(); //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='Range = {x} miles, Capacity = {value} tons'"); }
// // Create chart // private void createChart(RazorChartViewer viewer) { // The data for the chart double[] data = { 40, 45, 37, 24, 32, 39, 53, 52, 63, 49, 46, 40, 54, 50, 57, 57, 48, 49, 63, 67, 74, 72, 70, 89, 74 }; string[] labels = { "0\nJun 4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "0\nJun 5" }; // Create a XYChart object of size 400 x 270 pixels XYChart c = new XYChart(400, 270); // Set the plotarea at (80, 60) and of size 300 x 200 pixels. Turn off the grid lines by // setting their colors to Transparent. c.setPlotArea(80, 28, 300, 200).setGridColor(Chart.Transparent); // Add a title to the y axis ChartDirector.TextBox textbox = c.yAxis().setTitle("Temperature"); // Set the y axis title upright (font angle = 0) textbox.setFontAngle(0); // Put the y axis title on top of the axis textbox.setAlignment(Chart.TopLeft2); // Add green (0x99ff99), yellow (0xffff99) and red (0xff9999) zones to the y axis to // represent the ranges 0 - 50, 50 - 80, and 80 - max. c.yAxis().addZone(0, 50, 0x99ff99); c.yAxis().addZone(50, 80, 0xffff99); c.yAxis().addZone(80, 9999, 0xff9999); // Add a purple (0x800080) mark at y = 70 using a line width of 2. c.yAxis().addMark(70, 0x800080, "Alert = 70").setLineWidth(2); // Add a green (0x008000) mark at y = 40 using a line width of 2. c.yAxis().addMark(40, 0x008000, "Watch = 40").setLineWidth(2); // Add a legend box at (165, 0) (top right of the chart) using 8pt Arial font. and horizontal // layout. LegendBox legend = c.addLegend(165, 0, false, "Arial Bold", 8); // Disable the legend box boundary by setting the colors to Transparent legend.setBackground(Chart.Transparent, Chart.Transparent); // Add 3 custom entries to the legend box to represent the 3 zones legend.addKey("Normal", 0x80ff80); legend.addKey("Warning", 0xffff80); legend.addKey("Critical", 0xff8080); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. Show minor ticks for remaining labels. c.xAxis().setLabelStep(3, 1); // Add a 3D bar layer with the given data BarLayer layer = c.addBarLayer(data, 0xbbbbff); // Set the bar gap to 0 so that the bars are packed tightly layer.setBarGap(0); // Set the border color of the bars same as the fill color, with 1 pixel 3D border effect. layer.setBorderColor(Chart.SameAsMainColor, 1); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='Temperature at {x}:00 = {value} C'"); }
//Main code for creating chart. //Note: the argument img is unused because this demo only has 1 chart. public void createChart(WinChartViewer viewer, string img) { // The age groups string[] labels = { "0 - 4", "5 - 9", "10 - 14", "15 - 19", "20 - 24", "24 - 29", "30 - 34", "35 - 39", "40 - 44", "44 - 49","50 - 54", "55 - 59", "60 - 64", "65 - 69", "70 - 74", "75 - 79","80+" }; // The male population (in thousands) double[] male = { 215, 238, 225, 236, 235, 260, 286, 340, 363, 305, 259, 164, 135, 127, 102, 68, 66 }; // The female population (in thousands) double[] female = { 194, 203, 201, 220, 228, 271, 339, 401, 384, 304, 236, 137, 116, 122, 112, 85, 110 }; //============================================================= // Draw the right bar chart //============================================================= // Create a XYChart object of size 320 x 300 pixels XYChart c = new XYChart(320, 300); // Set the plotarea at (50, 0) and of size 250 x 255 pixels. Use pink // (0xffdddd) as the background. c.setPlotArea(50, 0, 250, 255, 0xffdddd); // Add a custom text label at the top right corner of the right bar chart c.addText(300, 0, "Female", "Times New Roman Bold Italic", 12, 0xa07070 ).setAlignment(Chart.TopRight); // Add the pink (0xf0c0c0) bar chart layer using the female data BarLayer femaleLayer = c.addBarLayer(female, 0xf0c0c0, "Female"); // Swap the axis so that the bars are drawn horizontally c.swapXY(true); // Set the bar to touch each others femaleLayer.setBarGap(Chart.TouchBar); // Set the border style of the bars to 1 pixel 3D border femaleLayer.setBorderColor(-1, 1); // Add a Transparent line layer to the chart using the male data. As it // is Transparent, only the female bar chart can be seen. We need to put // both male and female data in both left and right charts, because we // want auto-scaling to produce the same scale for both chart. c.addLineLayer(male, Chart.Transparent); // Set the y axis label font to Arial Bold c.yAxis().setLabelStyle("Arial Bold"); // Set the labels between the two bar charts, which can be considered as // the x-axis labels for the right chart ChartDirector.TextBox tb = c.xAxis().setLabels(labels); // Use a fix width of 50 for the labels (height = automatic) with center // alignment tb.setSize(50, 0); tb.setAlignment(Chart.Center); // Set the label font to Arial Bold tb.setFontStyle("Arial Bold"); // Disable ticks on the x-axis by setting the tick length to 0 c.xAxis().setTickLength(0); //============================================================= // Draw the left bar chart //============================================================= // Create a XYChart object of size 280 x 300 pixels with a transparent // background. XYChart c2 = new XYChart(280, 300, Chart.Transparent); // Set the plotarea at (20, 0) and of size 250 x 255 pixels. Use pale // blue (0xddddff) as the background. c2.setPlotArea(20, 0, 250, 255, 0xddddff); // Add a custom text label at the top left corner of the left bar chart c2.addText(20, 0, "Male", "Times New Roman Bold Italic", 12, 0x7070a0); // Add the pale blue (0xaaaaff) bar chart layer using the male data BarLayer maleLayer = c2.addBarLayer(male, 0xaaaaff, "Male"); // Swap the axis so that the bars are drawn horizontally c2.swapXY(true); // Reverse the direction of the y-axis so it runs from right to left c2.yAxis().setReverse(); // Set the bar to touch each others maleLayer.setBarGap(Chart.TouchBar); // Set the border style of the bars to 1 pixel 3D border maleLayer.setBorderColor(-1, 1); // Add a Transparent line layer to the chart using the female data. As it // is Transparent, only the male bar chart can be seen. We need to put // both male and female data in both left and right charts, because we // want auto-scaling to produce the same scale for both chart. c2.addLineLayer(female, Chart.Transparent); // Set the y axis label font to Arial Bold c2.yAxis().setLabelStyle("Arial Bold"); // Set the x-axis labels for tool tip purposes. c2.xAxis().setLabels(labels); // Hide the x-axis labels by setting them to Transparent. We only need to // display the x-axis labels for the right chart. c2.xAxis().setColors(0x000000, Chart.Transparent, -1, Chart.Transparent); //============================================================= // Use a MultiChart to contain both bar charts //============================================================= // Create a MultiChart object of size 590 x 320 pixels. MultiChart m = new MultiChart(590, 320); // Add a title to the chart using Arial Bold Italic font m.addTitle("Demographics Hong Kong Year 2002", "Arial Bold Italic"); // Add another title at the bottom using Arial Bold Italic font m.addTitle2(Chart.Bottom, "Population (in thousands)", "Arial Bold Italic", 10); // Put the right chart at (270, 25) m.addChart(270, 25, c); // Put the left chart at (0, 25) m.addChart(0, 25, c2); // Output the chart viewer.Image = m.makeImage(); //include tool tip for the chart viewer.ImageMap = m.getHTMLImageMap("clickable", "", "title='{dataSetName} (Age {xLabel}): Population {value}K'"); }
// // 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; }
// // Create chart // private void createChart(RazorChartViewer viewer) { // The data for the line chart double[] data0 = { 50, 55, 47, 36, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 84 }; double[] data1 = { 36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24, 28, 36, 30, 45, 46, 42, 48, 45, 43, 52, 64, 70 }; // The labels for the line chart string[] labels = { "Jan-04", "Feb-04", "Mar-04", "Apr-04", "May-04", "Jun-04", "Jul-04", "Aug-04", "Sep-04", "Oct-04", "Nov-04", "Dec-04", "Jan-05", "Feb-05","Mar-05", "Apr-05", "May-05", "Jun-05", "Jul-05", "Aug-05", "Sep-05", "Oct-05", "Nov-05","Dec-05" }; // Create an XYChart object of size 600 x 360 pixels, with a light blue (EEEEFF) background, // black border, 1 pxiel 3D border effect and rounded corners XYChart c = new XYChart(600, 360, 0xeeeeff, 0x000000, 1); c.setRoundedFrame(); // Set plotarea at (55, 60) with size of 520 x 240 pixels. Use white (ffffff) as background // and grey (cccccc) for grid lines c.setPlotArea(55, 60, 520, 240, 0xffffff, -1, -1, 0xcccccc, 0xcccccc); // Add a legend box at (55, 58) (top of plot area) using 9pt Arial Bold font with horizontal // layout Set border and background colors of the legend box to Transparent LegendBox legendBox = c.addLegend(55, 58, false, "Arial Bold", 9); legendBox.setBackground(Chart.Transparent); // Reserve 10% margin at the top of the plot area during auto-scaling to leave space for the // legends. c.yAxis().setAutoScale(0.1); // Add a title to the chart using 15pt Times Bold Italic font. The text is white (ffffff) on // a blue (0000cc) background, with glass effect. ChartDirector.TextBox title = c.addTitle("Monthly Revenue for Year 2000/2001", "Times New Roman Bold Italic", 15, 0xffffff); title.setBackground(0x0000cc, 0x000000, Chart.glassEffect(Chart.ReducedGlare)); // Add a title to the y axis c.yAxis().setTitle("Month Revenue (USD millions)"); // Set the labels on the x axis. Draw the labels vertical (angle = 90) c.xAxis().setLabels(labels).setFontAngle(90); // Add a vertical mark at x = 17 using a semi-transparent purple (809933ff) color and Arial // Bold font. Attached the mark (and therefore its label) to the top x axis. Mark mark = c.xAxis2().addMark(17, unchecked ((int)0x809933ff), "Merge with Star Tech", "Arial Bold"); // Set the mark line width to 2 pixels mark.setLineWidth(2); // Set the mark label font color to purple (0x9933ff) mark.setFontColor(0x9933ff); // Add a copyright message at (575, 295) (bottom right of plot area) using Arial Bold font ChartDirector.TextBox copyRight = c.addText(575, 295, "(c) Copyright Space Travel Ltd", "Arial Bold"); copyRight.setAlignment(Chart.BottomRight); // Add a line layer to the chart LineLayer layer = c.addLineLayer(); // Set the default line width to 3 pixels layer.setLineWidth(3); // Add the data sets to the line layer layer.addDataSet(data0, -1, "Enterprise"); layer.addDataSet(data1, -1, "Consumer"); // Create the image viewer.Image = c.makeWebImage(Chart.PNG); // Create an image map for the chart string chartImageMap = c.getHTMLImageMap(Url.Action("", "xystub"), "", "title='{dataSetName} @ {xLabel} = USD {value|0} millions'"); // Create an image map for the legend box string legendImageMap = legendBox.getHTMLImageMap( "javascript:popMsg('the legend key [{dataSetName}]');", " ", "title='This legend key is clickable!'"); // Obtain the image map for the title string titleImageMap = "<area " + title.getImageCoor() + " href='javascript:popMsg(\"the chart title\");' title='The title is clickable!'>"; // Obtain the image map for the mark string markImageMap = "<area " + mark.getImageCoor() + " href='javascript:popMsg(\"the Merge with Star Tech mark\");' title='The Merge with " + "Star Tech text is clickable!'>"; // Obtain the image map for the copyright message string copyRightImageMap = "<area " + copyRight.getImageCoor() + " href='javascript:popMsg(\"the copyright message\");' title='The copyright text is " + "clickable!'>"; // Get the combined image map viewer.ImageMap = chartImageMap + legendImageMap + titleImageMap + markImageMap + copyRightImageMap; }