// // Draw track line with data labels // private void trackLineLabel(XYChart c) { // Clear the current dynamic layer and get the DrawArea object to draw on it. DrawArea d = c.initDynamicLayer(); // In this example, we have two track lines. const int trackLineCount = 2; if (trackLinePos.Count == 0) { // Initialize the track line position by distributing them on the plot area PlotArea p = c.getPlotArea(); for (int i = 0; i < trackLineCount; ++i) { trackLinePos.Add(p.getLeftX() + (int)(p.getWidth() * (i + 0.5) / trackLineCount)); } } // Record the positions with the track lines var trackLineLog = new Dictionary <string, double> [trackLineCount]; // Draw the track lines if enabled drawTrackLine(c, trackLinePos[0], trackLineLog[0] = new Dictionary <string, double>()); drawTrackLine(c, trackLinePos[1], trackLineLog[1] = new Dictionary <string, double>()); // Draw the differences beteween the first two track lines drawTrackDiff(c, trackLineLog[0], trackLineLog[1]); }
// draw track line with data labels private void trackLineLabel(XYChart c, int mouseX) { DrawArea d = c.initDynamicLayer(); PlotArea plotArea = c.getPlotArea(); double xValue = c.getNearestXValue(mouseX); int xCoor = c.getXCoor(xValue); d.vline(plotArea.getTopY(), plotArea.getBottomY(), xCoor, d.dashLineColor(0x000000, 0x0101)); // container to hold the legned entries ArrayList legendEntries = new ArrayList(); for (int i = 0; i < c.getLayerCount(); ++i) { Layer layer = c.getLayerByZ(i); int xIndex = layer.getXIndexOf(xValue); // iterate through all the data sets in the layer for (int j = 0; j < layer.getDataSetCount(); ++j) { ChartDirector.DataSet dataSet = layer.getDataSetByZ(j); // we are only interested in visibale sets with names string dataName = dataSet.getDataName(); int color = dataSet.getDataColor(); if ((!string.IsNullOrEmpty(dataName)) && (color != Chart.Transparent)) { // Build the legend entry, consist of the legend icon, name and data value. double dataValue = dataSet.getValue(xIndex); legendEntries.Add("<*block*>" + dataSet.getLegendIcon() + " " + dataName + ": " + (( dataValue == Chart.NoValue) ? "N/A" : c.formatValue(dataValue, "{value|P4}")) + "<*/*>"); // Draw a track dot for data points within the plot area int yCoor = c.getYCoor(dataSet.getPosition(xIndex), dataSet.getUseYAxis()); if ((yCoor >= plotArea.getTopY()) && (yCoor <= plotArea.getBottomY())) { d.circle(xCoor, yCoor, 4, 4, color, color); } } } } // Create the legend by joining the legend entries legendEntries.Reverse(); string legendText = "<*block,maxWidth=" + plotArea.getWidth() + "*><*block*><*font=Arial Bold*>[" + c.xAxis().getFormattedLabel(xValue, "{value|P4") + "]<*/*> " + String.Join( " ", (string[])legendEntries.ToArray(typeof(string))) + "<*/*>"; // Display the legend on the top of the plot area TTFText t = d.text(legendText, "Arial", 8); t.draw(plotArea.getLeftX() + 5, plotArea.getTopY() - 3, 0x000000, Chart.BottomLeft); }
// // Create chart // private void createChart(RazorChartViewer viewer) { // Data points which more unevenly spaced in time double[] data0Y = { 62, 69, 53, 58, 84, 76, 49, 61, 64, 77, 79 }; DateTime[] data0X = { new DateTime(2007, 1, 1), new DateTime(2007, 1, 2), new DateTime(2007, 1, 5), new DateTime(2007, 1, 7), new DateTime(2007, 1,10), new DateTime(2007, 1, 14), new DateTime(2007, 1, 17), new DateTime(2007, 1, 18), new DateTime(2007, 1, 19), new DateTime(2007, 1, 20), new DateTime(2007, 1, 21) }; // Data points which are evenly spaced in a certain time range double[] data1Y = { 36, 25, 28, 38, 20, 30, 27, 35, 65, 60, 40, 73, 62, 90, 75, 72 }; DateTime data1Start = new DateTime(2007, 1, 1); DateTime data1End = new DateTime(2007, 1, 16); // Data points which are evenly spaced in another time range, in which the spacing is // different from the above series double[] data2Y = { 25, 15, 30, 23, 32, 55, 45 }; DateTime data2Start = new DateTime(2007, 1, 9); DateTime data2End = new DateTime(2007, 1, 21); // Create a XYChart object of size 600 x 400 pixels. Use a vertical gradient color from light // blue (99ccff) to white (ffffff) spanning the top 100 pixels as background. Set border to // grey (888888). Use rounded corners. Enable soft drop shadow. XYChart c = new XYChart(600, 400); c.setBackground(c.linearGradientColor(0, 0, 0, 100, 0x99ccff, 0xffffff), 0x888888); c.setRoundedFrame(); c.setDropShadow(); // Add a title using 18pt Times New Roman Bold Italic font. Set top margin to 16 pixels. c.addTitle("Product Line Order Backlog", "Times New Roman Bold Italic", 18).setMargin2(0, 0, 16, 0); // Set the plotarea at (60, 80) and of 510 x 275 pixels in size. Use transparent border and // dark grey (444444) dotted grid lines PlotArea plotArea = c.setPlotArea(60, 80, 510, 275, -1, -1, Chart.Transparent, c.dashLineColor(0x444444, 0x0101), -1); // Add a legend box where the top-center is anchored to the horizontal center of the plot // area at y = 45. Use horizontal layout and 10 points Arial Bold font, and transparent // background and border. LegendBox legendBox = c.addLegend(plotArea.getLeftX() + plotArea.getWidth() / 2, 45, false, "Arial Bold", 10); legendBox.setAlignment(Chart.TopCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // Set x-axis tick density to 75 pixels and y-axis tick density to 30 pixels. ChartDirector // auto-scaling will use this as the guidelines when putting ticks on the x-axis and y-axis. c.yAxis().setTickDensity(30); c.xAxis().setTickDensity(75); // Set all axes to transparent c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); // Set the x-axis margins to 15 pixels, so that the horizontal grid lines can extend beyond // the leftmost and rightmost vertical grid lines c.xAxis().setMargin(15, 15); // Set axis label style to 8pt Arial Bold c.xAxis().setLabelStyle("Arial Bold", 8); c.yAxis().setLabelStyle("Arial Bold", 8); c.yAxis2().setLabelStyle("Arial Bold", 8); // Add axis title using 10pt Arial Bold Italic font c.yAxis().setTitle("Backlog in USD millions", "Arial Bold Italic", 10); // Add the first data series LineLayer layer0 = c.addLineLayer2(); layer0.addDataSet(data0Y, 0xff0000, "Quantum Computer").setDataSymbol( Chart.GlassSphere2Shape, 11); layer0.setXData(data0X); layer0.setLineWidth(3); // Add the second data series LineLayer layer1 = c.addLineLayer2(); layer1.addDataSet(data1Y, 0x00ff00, "Atom Synthesizer").setDataSymbol( Chart.GlassSphere2Shape, 11); layer1.setXData2(data1Start, data1End); layer1.setLineWidth(3); // Add the third data series LineLayer layer2 = c.addLineLayer2(); layer2.addDataSet(data2Y, 0xff6600, "Proton Cannon").setDataSymbol(Chart.GlassSphere2Shape, 11); layer2.setXData2(data2Start, data2End); layer2.setLineWidth(3); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("", "", "title='Backlog of {dataSetName} at {x|mm/dd/yyyy}: US$ {value}M'"); }
// // Draw the track line with legend // private void trackLineLegend(XYChart c, int mouseX) { // Clear the current dynamic layer and get the DrawArea object to draw on it. DrawArea d = c.initDynamicLayer(); // The plot area object PlotArea plotArea = c.getPlotArea(); // Get the data x-value that is nearest to the mouse, and find its pixel coordinate. double xValue = c.getNearestXValue(mouseX); int xCoor = c.getXCoor(xValue); // Draw a vertical track line at the x-position d.vline(plotArea.getTopY(), plotArea.getBottomY(), xCoor, 0xaaaaaa); // Container to hold the legend entries var legendEntries = new List <string>(); // Iterate through all layers to build the legend array for (int i = 0; i < c.getLayerCount(); ++i) { Layer layer = c.getLayerByZ(i); // The data array index of the x-value int xIndex = layer.getXIndexOf(xValue); // Iterate through all the data sets in the layer for (int j = 0; j < layer.getDataSetCount(); ++j) { ChartDirector.DataSet dataSet = layer.getDataSetByZ(j); // We are only interested in visible data sets with names string dataName = dataSet.getDataName(); int color = dataSet.getDataColor(); if ((!string.IsNullOrEmpty(dataName)) && (color != Chart.Transparent)) { // Build the legend entry, consist of the legend icon, name and data value. double dataValue = dataSet.getValue(xIndex); legendEntries.Add("<*block*>" + dataSet.getLegendIcon() + " " + dataName + ": " + (( dataValue == Chart.NoValue) ? "N/A" : c.formatValue(dataValue, "{value|P4}")) + "<*/*>"); // Draw a track dot for data points within the plot area int yCoor = c.getYCoor(dataSet.getPosition(xIndex), dataSet.getUseYAxis()); if ((yCoor >= plotArea.getTopY()) && (yCoor <= plotArea.getBottomY())) { d.circle(xCoor, yCoor, 4, 4, color, color); } } } } // Create the legend by joining the legend entries legendEntries.Reverse(); string legendText = "<*block,maxWidth=" + plotArea.getWidth() + "*><*block*><*font=Arial Bold*>[" + c.xAxis().getFormattedLabel(xValue, "mmm dd, yyyy") + "]<*/*> " + String.Join( " ", legendEntries) + "<*/*>"; // Display the legend on the top of the plot area TTFText t = d.text(legendText, "Arial Bold", 10); t.draw(plotArea.getLeftX() + 5, plotArea.getTopY() - 3, 0x000000, Chart.BottomLeft); }
//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 data for the line chart double[] data0 = { 42, 49, 33, 38, 64, 56, 29, 41, 44, 57, 59, 42 }; double[] data1 = { 65, 75, 47, 34, 42, 49, 73, 62, 90, 69, 66, 78 }; double[] data2 = { 36, 28, 25, 28, 38, 20, 22, 30, 25, 33, 30, 24 }; string[] labels = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // Create a XYChart object of size 600 x 400 pixels XYChart c = new XYChart(600, 400); // Add a title to the chart using 18 pts Times Bold Italic font ChartDirector.TextBox title = c.addTitle("Product Line Global Revenue", "Times New Roman Bold Italic", 18); // Tentatively set the plotarea at (50, 55) and of (chart_width - 100) x // (chart_height - 120) pixels in size. Use a vertical gradient color // from sky blue (aaccff) t0 light blue (f9f9ff) as background. Set both // horizontal and vertical grid lines to dotted semi-transprent black // (aa000000). PlotArea plotArea = c.setPlotArea(50, 55, c.getWidth() - 100, c.getHeight() - 120, c.linearGradientColor(0, 55, 0, 55 + c.getHeight() - 120, 0xaaccff, 0xf9fcff), -1, -1, c.dashLineColor( unchecked ((int)0xaa000000), Chart.DotLine), -1); // Add a legend box and anchored the top center at the horizontal center // of the chart, just under the title. Use 10pts Arial Bold as font, with // transparent background and border. LegendBox legendBox = c.addLegend(c.getWidth() / 2, title.getHeight(), false, "Arial Bold", 10); legendBox.setAlignment(Chart.TopCenter); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // Set y-axis title using 10 points Arial Bold Italic font, label style // to 8 points Arial Bold, and axis color to transparent c.yAxis().setTitle("Revenue (USD millions)", "Arial Bold Italic", 10); c.yAxis().setLabelStyle("Arial Bold", 8); c.yAxis().setColors(Chart.Transparent); // Set y-axis tick density to 30 pixels. ChartDirector auto-scaling will // use this as the guideline when putting ticks on the y-axis. c.yAxis().setTickDensity(30); // Add a line layer to the chart LineLayer layer = c.addLineLayer2(); // Set the line width to 3 pixels layer.setLineWidth(3); // Add the three data sets to the line layer, using circles, diamands and // X shapes as symbols layer.addDataSet(data0, 0xff0000, "Quantum Computer").setDataSymbol( Chart.CircleSymbol, 9); layer.addDataSet(data1, 0x00ff00, "Atom Synthesizer").setDataSymbol( Chart.DiamondSymbol, 11); layer.addDataSet(data2, 0xff6600, "Proton Cannon").setDataSymbol( Chart.Cross2Shape(), 11); // Set the x axis labels c.xAxis().setLabels(labels); // Convert the labels on the x-axis to a CDMLTable CDMLTable table = c.xAxis().makeLabelTable(); // Set the default top/bottom margins of the cells to 3 pixels table.getStyle().setMargin2(0, 0, 3, 3); // Use Arial Bold as the font for the first row table.getRowStyle(0).setFontStyle("Arial Bold"); // // We can add more information to the table. In this sample code, we add // the data series and the legend icons to the table. // // Add 3 more rows to the table. Set the background of the 1st and 3rd // rows to light grey (eeeeee). table.appendRow().setBackground(0xeeeeee, Chart.LineColor); table.appendRow(); table.appendRow().setBackground(0xeeeeee, Chart.LineColor); // Put the values of the 3 data series to the cells in the 3 rows for (int i = 0; i < data0.Length; ++i) { table.setText(i, 1, (data0[i]).ToString()); table.setText(i, 2, (data1[i]).ToString()); table.setText(i, 3, (data2[i]).ToString()); } // Insert a column on the left for the legend icons. Use 5 pixels // left/right margins and 3 pixels top/bottom margins for the cells in // this column. table.insertCol(0).setMargin2(5, 5, 3, 3); // The top cell is set to transparent, so it is invisible table.getCell(0, 0).setBackground(Chart.Transparent, Chart.Transparent); // The other 3 cells are set to the legend icons of the 3 data series table.setText(0, 1, layer.getLegendIcon(0)); table.setText(0, 2, layer.getLegendIcon(1)); table.setText(0, 3, layer.getLegendIcon(2)); // Layout legend box first, so we can get its size c.layoutLegend(); // Adjust the plot area size, such that the bounding box (inclusive of // axes) is 2 pixels from the left, right and bottom edge, and is just // under the legend box. c.packPlotArea(2, legendBox.getTopY() + legendBox.getHeight(), c.getWidth() - 3, c.getHeight() - 3); // 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.Image = c.makeImage(); //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='Revenue of {dataSetName} in {xLabel}: US$ {value}M'"); }
//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[] data0 = { 410, 420, 500, 590 }; double[] data1 = { 500, 370, 680, 850 }; string[] labels = { "Q1", "Q2", "Q3", "Q4" }; // Create a XYChart object of size 600 x 400 pixels XYChart c = new XYChart(600, 400); // Add a title to the chart using 18pt Times Bold Italic font ChartDirector.TextBox title = c.addTitle("Product Line Global Revenue", "Times New Roman Bold Italic", 18); // Tentatively set the plotarea at (50, 55) and of (chart_width - 100) x (chart_height - // 150) pixels in size. Use a vertical gradient color from sky blue (aaccff) t0 light // blue (f9f9ff) as background. Set both horizontal and vertical grid lines to dotted // semi-transprent black (aa000000). PlotArea plotArea = c.setPlotArea(50, 55, c.getWidth() - 100, c.getHeight() - 150, c.linearGradientColor(0, 55, 0, 55 + c.getHeight() - 150, 0xaaccff, 0xf9fcff), -1, -1, c.dashLineColor(unchecked ((int)0xaa000000), Chart.DotLine), -1); // Set y-axis title using 12 points Arial Bold Italic font, and set its position 10 // pixels from the axis. c.yAxis().setTitle("Revenue (USD millions)", "Arial Bold Italic", 12); c.yAxis().setTitlePos(Chart.Left, 10); // Set y-axis label style to 10 points Arial Bold and axis color to transparent c.yAxis().setLabelStyle("Arial Bold", 10); c.yAxis().setColors(Chart.Transparent); // Set y-axis tick density to 30 pixels. ChartDirector auto-scaling will use this as the // guideline when putting ticks on the y-axis. c.yAxis().setTickDensity(30); // Add a bar layer to the chart with side layout BarLayer layer = c.addBarLayer2(Chart.Side); // Add two data sets to the bar layer layer.addDataSet(data0, 0xff6600, "FY 2007"); layer.addDataSet(data1, 0x0088ff, "FY 2008"); // Use soft lighting effect with light direction from the left layer.setBorderColor(Chart.Transparent, Chart.softLighting(Chart.Left)); // Set the x axis labels c.xAxis().setLabels(labels); // Convert the labels on the x-axis to a CDMLTable CDMLTable table = c.xAxis().makeLabelTable(); // Set the default left/right margins to 5 pixels and top/bottom margins to 3 pixels. // Set the default font size to 10 points ChartDirector.TextBox cellStyle = table.getStyle(); cellStyle.setMargin2(5, 5, 4, 3); cellStyle.setFontSize(10); // Set the first row to use Arial Bold font, with a light grey (eeeeee) background. ChartDirector.TextBox firstRowStyle = table.getRowStyle(0); firstRowStyle.setFontStyle("Arial Bold"); firstRowStyle.setBackground(0xeeeeee, Chart.LineColor); // // We can add more information to the table. In this sample code, we add the data series // and the legend icons to the table. // // Add 3 more rows to the table. Set the background of the 2nd row to light grey // (eeeeee). table.appendRow(); table.appendRow().setBackground(0xeeeeee, Chart.LineColor); table.appendRow(); // Put the values of the 2 data series in the first 2 rows. Put the percentage // differences in the 3rd row. for (int i = 0; i < data0.Length; ++i) { table.setText(i, 1, (data0[i]).ToString()); table.setText(i, 2, (data1[i]).ToString()); double percentageDiff = 100.0 * (data1[i] - data0[i]) / data0[i]; // Use red or green color depending on whether the difference is positive or // negative string formatString = "<*color=008800*>+{value|1}%"; if (percentageDiff < 0) { formatString = "<*color=cc0000*>{value|1}%"; } table.setText(i, 3, c.formatValue(percentageDiff, formatString)); } // Insert a column on the left for the legend icons using Arial Bold font. table.insertCol(0).setFontStyle("Arial Bold"); // The top cell is set to transparent, so it is invisible table.getCell(0, 0).setBackground(Chart.Transparent, Chart.Transparent); // The next 2 cells are set to the legend icons and names of the 2 data series table.setText(0, 1, layer.getLegendIcon(0) + " FY 2007"); table.setText(0, 2, layer.getLegendIcon(1) + " FY 2008"); // The last cell is set to "Change" table.setText(0, 3, "Change"); // Append a column on the right for the total values. table.appendCol(); // Put "Total" in the top cell as the heading of this column table.setText(table.getColCount() - 1, 0, "Total"); // The next two cells are the total of the data series double total0 = new ArrayMath(data0).sum(); double total1 = new ArrayMath(data1).sum(); table.setText(table.getColCount() - 1, 1, (total0).ToString()); table.setText(table.getColCount() - 1, 2, (total1).ToString()); // The last cell is the percentage differences of the total double totalPercentageDiff = (total1 - total0) / total0 * 100; // Use red or green color depending on whether the difference is positive or negative string totalFormatString = "<*color=008800*>+{value|1}%"; if (totalPercentageDiff < 0) { totalFormatString = "<*color=cc0000*>{value|1}%"; } table.setText(table.getColCount() - 1, 3, c.formatValue(totalPercentageDiff, totalFormatString)); // // We now demonstrate how to adjust the plot area positions, to allow space for the // newly inserted left and right columns in the table. // // We layout the axis first in order to get the axis metrics (including table metrics) c.layoutAxes(); // If the first column is wider than the left y-axis, we need to reserve for some left // margin to ensure the first column stays within the chart. int leftMargin = 0; if (table.getColWidth(0) > c.yAxis().getThickness()) { leftMargin = table.getColWidth(0) - c.yAxis().getThickness(); } // Similarly, we need to reserve some right margin for the last column int rightMargin = table.getColWidth(table.getColCount() - 1); // Adjust the plot area size, such that the bounding box (inclusive of axes) using the // given left and right margin, plus 2 more pixels. Put the plot area 10 pixels below // the title and use 2 pixels as the bottom margin. from the left, right and bottom // edge, and is just under the legend box. c.packPlotArea(leftMargin + 2, title.getHeight() + 10, c.getWidth() - 3 - rightMargin, c.getHeight() - 3); // After determining the exact plot area position, we may adjust title position so that // it is centered relative to the plot area (instead of the chart) 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='Revenue in {xLabel} {dataSetName}: US$ {value}M'"); }
//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}'"); }
//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 area chart double[] data = { 30, 28, 40, 55, 75, 68, 54, 60, 50, 62, 75, 65, 75, 89, 60, 55, 53, 35, 50, 66, 56, 48, 52, 65, 62 }; // The labels for the area chart string[] labels = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24" }; // Create a XYChart object of size 600 x 360 pixels, with a brushed silver background, 1 // pixel 3D border effect, rounded corners and soft drop shadow. XYChart c = new XYChart(600, 360, Chart.brushedSilverColor(), Chart.Transparent, 1); c.setRoundedFrame(); c.setDropShadow(); // Add a title box to the chart using 18pt Times Bold Italic font. ChartDirector.TextBox title = c.addTitle( "<*block,valign=absmiddle*><*img=@/images/star.png*><*img=@/images/star.png*> " + "Performance Enhancer <*img=@/images/star.png*><*img=@/images/star.png*><*/*>", "Times New Roman Bold Italic", 18); // // Use a text box with a depressed 3D border to create the inner depressed region // // The width of the frame border int frameWidth = 5; // Set the depressed region position ChartDirector.TextBox contentBox = c.addText(frameWidth, title.getHeight(), ""); contentBox.setSize(c.getDrawArea().getWidth() - 1 - frameWidth * 2, c.getDrawArea( ).getHeight() - title.getHeight() - frameWidth - 1); // Use -1 as the rasied effect to create a depressed region contentBox.setBackground(Chart.Transparent, Chart.Transparent, -1); // Set rounded corners, and put the text box at the back of the chart contentBox.setRoundedCorners(10); contentBox.setZOrder(Chart.ChartBackZ); // Tentatively set the plotarea to 50 pixels from the left depressed edge, and 25 pixels // under the top depressed edge. Set the width to 75 pixels less than the depressed // region width, and the height to 75 pixels less than the depressed region height. Use // white (ffffff) background, transparent border, and grey (cccccc) horizontal and // vertical grid lines. PlotArea plotArea = c.setPlotArea(50 + contentBox.getLeftX(), contentBox.getTopY() + 25, contentBox.getWidth() - 75, contentBox.getHeight() - 75, 0xffffff, -1, -1, 0xcccccc, -1); // Add a title to the y axis c.yAxis().setTitle("Energy Concentration (KJ per liter)"); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Display 1 out of 3 labels on the x-axis. c.xAxis().setLabelStep(3); // Add a title to the x axis using CDML c.xAxis().setTitle( "<*block,valign=absmiddle*><*img=@/images/clock.png*> Elapsed Time (hour)<*/*>"); // Set the axes width to 2 pixels c.xAxis().setWidth(2); c.yAxis().setWidth(2); // Add an area layer to the chart using a gradient color that changes vertically from // semi-transparent red (60ff0000) to semi-transparent white (60ffffff) c.addAreaLayer(data, c.linearGradientColor(0, contentBox.getTopY() + 20, 0, contentBox.getTopY() + contentBox.getHeight() - 50, 0x60ff0000, 0x60ffffff)); // Adjust the plot area size, such that the bounding box (inclusive of axes) is 15 // pixels from the left depressed edge, 25 pixels below the top depressed edge, 25 // pixels from the right depressed edge, and 15 pixels above the bottom depressed edge. c.packPlotArea(contentBox.getLeftX() + 15, contentBox.getTopY() + 25, contentBox.getLeftX() + contentBox.getWidth() - 25, contentBox.getTopY() + contentBox.getHeight() - 15); // Add a custom CDML text with the bottom right corner is anchored to the bootom right // corner of the plot area, with 5 pixels margin. c.addText(plotArea.getLeftX() + plotArea.getWidth() - 5, plotArea.getTopY() + plotArea.getHeight() - 5, "<*block,valign=absmiddle*><*img=@/images/small_molecule.png*> <*block*>" + "<*font=Times New Roman Bold Italic,size=10,color=804040*>Molecular\nEngineering" + "<*/*>").setAlignment(Chart.BottomRight); // Output the chart viewer.Chart = c; //include tool tip for the chart viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='Hour {xLabel}: {value} KJ/liter'"); }
// // Draw finance chart track line with legend // private void trackFinance(MultiChart m, int mouseX) { // Clear the current dynamic layer and get the DrawArea object to draw on it. DrawArea d = m.initDynamicLayer(); // It is possible for a FinanceChart to be empty, so we need to check for it. if (m.getChartCount() == 0) { return; } // Get the data x-value that is nearest to the mouse int xValue = (int)(((XYChart)m.getChart(0)).getNearestXValue(mouseX)); // Iterate the XY charts (main price chart and indicator charts) in the FinanceChart XYChart c = null; for (int i = 0; i < m.getChartCount(); ++i) { c = (XYChart)m.getChart(i); // Variables to hold the legend entries string ohlcLegend = ""; ArrayList legendEntries = new ArrayList(); // Iterate through all layers to find the highest data point for (int j = 0; j < c.getLayerCount(); ++j) { Layer layer = c.getLayerByZ(j); int xIndex = layer.getXIndexOf(xValue); int dataSetCount = layer.getDataSetCount(); // In a FinanceChart, only layers showing OHLC data can have 4 data sets if (dataSetCount == 4) { double highValue = layer.getDataSet(0).getValue(xIndex); double lowValue = layer.getDataSet(1).getValue(xIndex); double openValue = layer.getDataSet(2).getValue(xIndex); double closeValue = layer.getDataSet(3).getValue(xIndex); if (closeValue != Chart.NoValue) { // Build the OHLC legend ohlcLegend = "Open: " + c.formatValue(openValue, "{value|P4}") + ", High: " + c.formatValue(highValue, "{value|P4}") + ", Low: " + c.formatValue(lowValue, "{value|P4}") + ", Close: " + c.formatValue(closeValue, "{value|P4}"); // We also draw an upward or downward triangle for up and down days and the % // change double lastCloseValue = layer.getDataSet(3).getValue(xIndex - 1); if (lastCloseValue != Chart.NoValue) { double change = closeValue - lastCloseValue; double percent = change * 100 / closeValue; string symbol = ((change >= 0) ? "<*font,color=008800*><*img=@triangle,width=8,color=008800*>" : "<*font,color=CC0000*><*img=@invertedtriangle,width=8,color=CC0000*>"); ohlcLegend = ohlcLegend + " " + symbol + " " + c.formatValue(change, "{value|P4}") + " (" + c.formatValue(percent, "{value|2}") + "%)<*/font*>" ; } // Use a <*block*> to make sure the line does not wrap within the legend entry ohlcLegend = "<*block*>" + ohlcLegend + " <*/*>"; } } else { // Iterate through all the data sets in the layer for (int k = 0; k < layer.getDataSetCount(); ++k) { ChartDirector.DataSet dataSet = layer.getDataSetByZ(k); string name = dataSet.getDataName(); double value = dataSet.getValue(xIndex); if ((!string.IsNullOrEmpty(name)) && (value != Chart.NoValue)) { // In a FinanceChart, the data set name consists of the indicator name and its // latest value. It is like "Vol: 123M" or "RSI (14): 55.34". As we are // generating the values dynamically, we need to extract the indictor name // out, and also the volume unit (if any). // The unit character, if any, is the last character and must not be a digit. string unitChar = name.Substring(name.Length - 1); if (unitChar.CompareTo("0") >= 0 && unitChar.CompareTo("9") <= 0) { unitChar = ""; } // The indicator name is the part of the name up to the colon character. int delimiterPosition = name.IndexOf(":"); if (delimiterPosition != -1) { name = name.Substring(0, delimiterPosition); } // In a FinanceChart, if there are two data sets, it must be representing a // range. if (dataSetCount == 2) { // We show both values in the range in a single legend entry value = layer.getDataSet(0).getValue(xIndex); double value2 = layer.getDataSet(1).getValue(xIndex); name = name + ": " + c.formatValue(Math.Min(value, value2), "{value|P3}") + " - " + c.formatValue(Math.Max(value, value2), "{value|P3}"); } else { // In a FinanceChart, only the layer for volume bars has 3 data sets for // up/down/flat days if (dataSetCount == 3) { // The actual volume is the sum of the 3 data sets. value = layer.getDataSet(0).getValue(xIndex) + layer.getDataSet(1 ).getValue(xIndex) + layer.getDataSet(2).getValue(xIndex); } // Create the legend entry name = name + ": " + c.formatValue(value, "{value|P3}") + unitChar; } // Build the legend entry, consist of a colored square box and the name (with // the data value in it). legendEntries.Add("<*block*><*img=@square,width=8,edgeColor=000000,color=" + dataSet.getDataColor().ToString("x") + "*> " + name + "<*/*>"); } } } } // Get the plot area position relative to the entire FinanceChart PlotArea plotArea = c.getPlotArea(); int plotAreaLeftX = plotArea.getLeftX() + c.getAbsOffsetX(); int plotAreaTopY = plotArea.getTopY() + c.getAbsOffsetY(); // The legend is formed by concatenating the legend entries. legendEntries.Reverse(); string legendText = String.Join(" ", (string[])legendEntries.ToArray(typeof(string))); // Add the date and the ohlcLegend (if any) at the beginning of the legend legendText = "<*block,valign=top,maxWidth=" + (plotArea.getWidth() - 5) + "*><*font=Arial Bold*>[" + c.xAxis().getFormattedLabel(xValue, "mmm dd, yyyy") + "]<*/font*> " + ohlcLegend + legendText; // Draw a vertical track line at the x-position d.vline(plotAreaTopY, plotAreaTopY + plotArea.getHeight(), c.getXCoor(xValue) + c.getAbsOffsetX(), d.dashLineColor(0x000000, 0x0101)); // Display the legend on the top of the plot area TTFText t = d.text(legendText, "Arial", 8); t.draw(plotAreaLeftX + 5, plotAreaTopY + 3, 0x000000, Chart.TopLeft); } }