Whether or not it is a Console App, you can run "headless" applications that exists only on a server without a Windows display, serving up images of charts. You will need to add System.Windows.Forms and System.Drawing references to the program, in addition to our QCChart2DNet.DLL. And you will need add the following using references in addition to those included in a standard Console app:
using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using com.quinncurtis.chart2dnet;
You create and size a ChartView object, without placing it in a form, and draw the chart to it. You then render the chart as an image file using our BufferedImage class.
The Console Application program below, producing the same chart as in our FinancialExamples.CandlestickChart example program, produced this jpeg file.
 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Data; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using com.quinncurtis.chart2dnet;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { InitializeChart(); }
static void InitializeChart() { ChartView chartVu = new ChartView(); chartVu.Size = new Size(600, 300);
int nNumPnts = 50, nNumGroups = 4; int weekmode = ChartObj.WEEK_5D; ChartCalendar[] xValues = new ChartCalendar[nNumPnts]; double[,] stockPriceData = new double[nNumGroups, nNumPnts]; Font theFont;
double minval = 0.0, maxval = 0.0; int i;
ChartCalendar currentdate = new ChartCalendar(); ChartCalendar.SetTOD(currentdate, 0, 0, 1); theFont = new Font("Microsoft Sans Serif", 10, FontStyle.Bold); currentdate = ChartCalendar.CalendarDaysAdd(currentdate, 1, weekmode); // Make sure not to start on a weekend xValues[0] = (ChartCalendar)currentdate.Clone(); currentdate = ChartCalendar.CalendarDaysAdd(currentdate, 1, weekmode); stockPriceData[3, 0] = 25; // close stockPriceData[0, 0] = 25; // open stockPriceData[1, 0] = 26; // high stockPriceData[2, 0] = 24; // low
for (i = 1; i < nNumPnts; i++) { xValues[i] = (ChartCalendar)currentdate.Clone(); stockPriceData[3, i] += stockPriceData[3, i - 1] + 3 * (0.52 - ChartSupport.GetRandomDouble()); // close stockPriceData[0, i] += stockPriceData[3, i] + 2 * (0.5 - ChartSupport.GetRandomDouble()); // open minval = Math.Min(stockPriceData[3, i], stockPriceData[0, i]); maxval = Math.Max(stockPriceData[3, i], stockPriceData[0, i]); stockPriceData[1, i] = maxval + 1.5 * ChartSupport.GetRandomDouble(); // high stockPriceData[2, i] = minval - 1.5 * ChartSupport.GetRandomDouble(); // low currentdate = ChartCalendar.CalendarDaysAdd(currentdate, 1, weekmode); }
TimeGroupDataset Dataset1 = new TimeGroupDataset("Stock Data", xValues, stockPriceData); int[] compressarray = new int[4]; compressarray[0] = ChartObj.DATACOMPRESS_AVERAGE; compressarray[1] = ChartObj.DATACOMPRESS_MAX; compressarray[2] = ChartObj.DATACOMPRESS_MIN; compressarray[3] = ChartObj.DATACOMPRESS_AVERAGE;
TimeCoordinates pTransform1 = new TimeCoordinates(); pTransform1.SetWeekType(ChartObj.WEEK_5D); pTransform1.AutoScale(Dataset1, ChartObj.AUTOAXES_NEAR, ChartObj.AUTOAXES_NEAR); pTransform1.SetGraphBorderDiagonal(0.13, .15, .90, 0.8);
Background graphbackground1 = new Background(pTransform1, ChartObj.GRAPH_BACKGROUND, Color.FromArgb(127, 127, 127), Color.FromArgb(55, 55, 55), ChartObj.Y_AXIS); chartVu.AddChartObject(graphbackground1); Background plotbackground1 = new Background(pTransform1, ChartObj.PLOT_BACKGROUND, Color.White); chartVu.AddChartObject(plotbackground1);
TimeAxis xAxis1 = new TimeAxis(pTransform1); xAxis1.SetColor(Color.White); chartVu.AddChartObject(xAxis1);
LinearAxis yAxis1 = new LinearAxis(pTransform1, ChartObj.Y_AXIS); yAxis1.SetColor(Color.White); chartVu.AddChartObject(yAxis1);
TimeAxisLabels xAxisLab1 = new TimeAxisLabels(xAxis1); xAxisLab1.SetColor(Color.White); chartVu.AddChartObject(xAxisLab1);
NumericAxisLabels yAxisLab1 = new NumericAxisLabels(yAxis1); yAxisLab1.SetAxisLabelsFormat(ChartObj.CURRENCYFORMAT); yAxisLab1.SetColor(Color.White); chartVu.AddChartObject(yAxisLab1);
Grid xgrid1 = new Grid(xAxis1, yAxis1, ChartObj.X_AXIS, ChartObj.GRID_MAJOR); xgrid1.SetColor(Color.Black); chartVu.AddChartObject(xgrid1);
Grid xgrid2 = new Grid(xAxis1, yAxis1, ChartObj.X_AXIS, ChartObj.GRID_MINOR); xgrid2.SetColor(Color.Gray); chartVu.AddChartObject(xgrid2);
Grid ygrid1 = new Grid(xAxis1, yAxis1, ChartObj.Y_AXIS, ChartObj.GRID_MAJOR); ygrid1.SetColor(Color.Black); chartVu.AddChartObject(ygrid1);
Grid ygrid2 = new Grid(xAxis1, yAxis1, ChartObj.Y_AXIS, ChartObj.GRID_MINOR); ygrid2.SetColor(Color.Gray); chartVu.AddChartObject(ygrid2);
ChartAttribute defaultattrib = new ChartAttribute(Color.Black, 1, DashStyle.Solid, Color.White); defaultattrib.SetFillFlag(true); ChartAttribute fillattrib = new ChartAttribute(Color.Black, 1, DashStyle.Solid, Color.Red); fillattrib.SetFillFlag(true); CandlestickPlot thePlot1 = new CandlestickPlot(pTransform1, Dataset1, ChartCalendar.GetCalendarWidthValue(ChartObj.DAY_OF_YEAR, 0.8), defaultattrib, fillattrib);
chartVu.AddChartObject(thePlot1);
Font theTitleFont = new Font("Microsoft Sans Serif", 18, FontStyle.Bold); ChartTitle mainTitle = new ChartTitle(pTransform1, theTitleFont, "Candlestick Plots in Technical Analysis"); mainTitle.SetTitleType(ChartObj.CHART_HEADER); mainTitle.SetTitlePosition(ChartObj.CENTER_GRAPH); mainTitle.SetColor(Color.White); chartVu.AddChartObject(mainTitle);
Font theFooterFont = new Font("Microsoft Sans Serif", 10, FontStyle.Bold); ChartTitle footer = new ChartTitle(pTransform1, theFooterFont, "The Open-Close box is filled if the open price is greater than the close price."); footer.SetTitleType(ChartObj.CHART_FOOTER); footer.SetTitlePosition(ChartObj.CENTER_GRAPH); footer.SetTitleOffset(8); footer.SetColor(Color.White); chartVu.AddChartObject(footer);
// Save the chart as an image file BufferedImage chartimage = new BufferedImage(chartVu, ImageFormat.Jpeg); chartimage.SaveImage("CandlestickPlot.jpg");
} } } |