// // 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 350 pixels XYChart c = new XYChart(640, 350); // 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 to the chart using 18pt Times New Roman Bold Italic font c.addTitle(" Zooming and Scrolling with Track Line", "Times New Roman Bold Italic", 18); // Set the axis stem to transparent c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); // Add axis title using 10pt 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 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(); // Add up to 3 data series to a line layer, depending on whether the user has selected the // data series. layer.setXData(timeStamps); if (viewer.GetCustomAttr("data0CheckBox") != "F") { layer.addDataSet(dataSeriesA, 0xff3333, "Alpha Series"); } if (viewer.GetCustomAttr("data1CheckBox") != "F") { layer.addDataSet(dataSeriesB, 0x008800, "Beta Series"); } if (viewer.GetCustomAttr("data2CheckBox") != "F") { layer.addDataSet(dataSeriesC, 0x3333cc, "Gamma Series"); } //================================================================================ // 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}"); //================================================================================ // Step 5 - Output the chart //================================================================================ // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Output Javascript chart model to the browser to suppport tracking cursor viewer.ChartModel = c.getJsChartModel(); }
// // Create chart // private void createChart(RazorChartViewer viewer) { // 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); // Add a title to the chart using 20pt Arial font c.addTitle(" Plasma Stabilizer Energy Usage", "Arial", 20); // Set the plotarea at (55, 60) and of size 560 x 330 pixels, with transparent background and // border and light grey (0xcccccc) horizontal grid lines c.setPlotArea(55, 60, 560, 330, -1, -1, Chart.Transparent, 0xcccccc); // Add a legend box at (55, 30) using horizontal layout, with 10pt Arial Bold as font and // transparent background and border. c.addLegend(55, 30, false, "Arial Bold", 10).setBackground(Chart.Transparent); // Set axis label style to 10pt Arial c.xAxis().setLabelStyle("Arial", 10); c.yAxis().setLabelStyle("Arial", 10); // Set the x and y axis stems to transparent, and the x-axis tick color to grey (0xcccccc) c.xAxis().setColors(Chart.Transparent, Chart.TextColor, Chart.TextColor, 0xcccccc); c.yAxis().setColors(Chart.Transparent); // Configure the x-axis tick lengtht to 10 pixels internal to the plot area c.xAxis().setTickLength(-10, 0); // With the ticks internal to the plot area, the x-axis labels will come very close to the // axis stem, so we configure a wider gap. c.xAxis().setLabelGap(10); // 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.Image = c.makeWebImage(Chart.PNG); // Output Javascript chart model to the browser to suppport tracking cursor viewer.ChartModel = c.getJsChartModel(); }
// // 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 270 pixels in size, with light grey (f4f4f4) background, // black (000000) border, 1 pixel raised effect, and with a rounded frame. XYChart c = new XYChart(600, 270, 0xf4f4f4, 0x000000, 0); c.setRoundedFrame(); // Set the plotarea at (55, 57) and of size 520 x 185 pixels. Use white (ffffff) background. // 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, 57, 520, 185, 0xffffff, -1, -1, 0xcccccc, 0xcccccc); c.setClipping(); // Add a title to the chart using 15pt Times New Roman Bold Italic font, with a light grey // (dddddd) background, black (000000) border, and a glass like raised effect. c.addTitle("Field Intensity at Observation Satellite", "Times New Roman Bold Italic", 15 ).setBackground(0xdddddd, 0x000000, Chart.glassEffect()); // Configure the y-axis with a 10pt Arial Bold axis title c.yAxis().setTitle("Intensity (V/m)", "Arial Bold", 10); // Configure the x-axis to auto-scale with at least 75 pixels between major tick and 15 // pixels between minor ticks. This shows more minor grid lines on the chart. c.xAxis().setTickDensity(75, 15); // Set the axes width to 2 pixels c.xAxis().setWidth(2); c.yAxis().setWidth(2); // 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, "Alpha"); layer.addDataSet(dataSeries2, 0x00cc00, "Beta"); layer.addDataSet(dataSeries3, 0x0000ff, "Gamma"); // Output the chart viewer.Image = c.makeWebImage(Chart.PNG); // Output Javascript chart model to the browser to suppport tracking cursor viewer.ChartModel = c.getJsChartModel(); }