// ******************************************************************** // Public Methods // ******************************************************************** #region Public Methods /// <summary> /// Constructor. Initializes class fields. /// </summary> public AdornerCursorCoordinateDrawer(XYLineChart xyLineChart, UIElement adornedElement, MatrixTransform shapeTransform) : base(adornedElement) { elementTransform = shapeTransform; IsHitTestVisible = false; this.xyLineChart = xyLineChart; }
// ******************************************************************** // Public Methods // ******************************************************************** #region Public Methods /// <summary> /// Constructor. Initializes class fields. /// </summary> public AdornerCursorCoordinateDrawer(XYLineChart xyLineChart, UIElement adornedElement, MatrixTransform shapeTransform) : base(adornedElement) { elementTransform = shapeTransform; IsHitTestVisible = false; this.xyLineChart = xyLineChart; }
// ******************************************************************** // Public Methods // ******************************************************************** #region Public Methods /// <summary> /// Copies the plotToCopy as a bitmap to the clipboard, and copies the /// chartControl to the clipboard as tab separated values. /// </summary> /// <param name="plotToCopy"></param> /// <param name="chartControl"></param> /// <param name="width">Width of the bitmap to be created</param> /// <param name="height">Height of the bitmap to be created</param> public static void CopyChartToClipboard(FrameworkElement plotToCopy, XYLineChart chartControl, double width, double height) { Bitmap bitmap = CopyFrameworkElementToBitmap(plotToCopy, width, height); string text = ConvertChartToSpreadsheetText(chartControl, '\t'); MemoryStream csv = new MemoryStream(Encoding.UTF8.GetBytes(ConvertChartToSpreadsheetText(chartControl, ','))); DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.Bitmap, bitmap); dataObject.SetData(DataFormats.Text, text); dataObject.SetData(DataFormats.CommaSeparatedValue, csv); Clipboard.SetDataObject(dataObject); }
// ******************************************************************** // Public Methods // ******************************************************************** #region Public Methods /// <summary> /// Copies the plotToCopy as a bitmap to the clipboard, and copies the /// chartControl to the clipboard as tab separated values. /// </summary> /// <param name="plotToCopy"></param> /// <param name="chartControl"></param> /// <param name="width">Width of the bitmap to be created</param> /// <param name="height">Height of the bitmap to be created</param> public static void CopyChartToClipboard(FrameworkElement plotToCopy, XYLineChart chartControl, double width, double height) { Bitmap bitmap = CopyFrameworkElementToBitmap(plotToCopy, width, height); string text = ConvertChartToSpreadsheetText(chartControl, '\t'); MemoryStream csv = new MemoryStream(Encoding.UTF8.GetBytes(ConvertChartToSpreadsheetText(chartControl, ','))); DataObject dataObject = new DataObject(); dataObject.SetData(DataFormats.Bitmap, bitmap); dataObject.SetData(DataFormats.Text, text); dataObject.SetData(DataFormats.CommaSeparatedValue, csv); Clipboard.SetDataObject(dataObject); }
/// <summary> /// Converts a chart to tab separated values /// </summary> /// <param name="xyLineChart"></param> /// <param name="token"></param> /// <returns></returns> public static string ConvertChartToSpreadsheetText(XYLineChart xyLineChart, char token) { int maxPrimitiveLength = 0; foreach (ChartPrimitive primitive in xyLineChart.Primitives) { maxPrimitiveLength = Math.Max(maxPrimitiveLength, primitive.Points.Count); } string[] grid = new string[maxPrimitiveLength + 1]; foreach (ChartPrimitive primitive in xyLineChart.Primitives) { if (primitive.ShowInLegend) { int row = 0; grid[row] += primitive.Label + " X" + token + primitive.Label + " Y" + token; foreach (Point point in primitive.Points) { ++row; grid[row] += point.X.ToString() + token + point.Y + token; } ++row; while (row < grid.Length) { grid[row] += token.ToString() + token; ++row; } } } StringBuilder sb = new StringBuilder(); sb.AppendLine(xyLineChart.Title); foreach (string line in grid) { sb.AppendLine(line.Substring(0, line.Length - 1)); } return(sb.ToString()); }
/// <summary> /// Converts a chart to tab separated values /// </summary> /// <param name="xyLineChart"></param> /// <param name="token"></param> /// <returns></returns> public static string ConvertChartToSpreadsheetText(XYLineChart xyLineChart, char token) { int maxPrimitiveLength = 0; foreach (ChartPrimitive primitive in xyLineChart.Primitives) { maxPrimitiveLength = Math.Max(maxPrimitiveLength, primitive.Points.Count); } string[] grid = new string[maxPrimitiveLength + 1]; foreach (ChartPrimitive primitive in xyLineChart.Primitives) { if (primitive.ShowInLegend) { int row = 0; grid[row] += primitive.Label + " X" + token + primitive.Label + " Y"+token; foreach (Point point in primitive.Points) { ++row; grid[row] += point.X.ToString() + token + point.Y + token; } ++row; while (row < grid.Length) { grid[row] += token.ToString()+token; ++row; } } } StringBuilder sb = new StringBuilder(); sb.AppendLine(xyLineChart.Title); foreach (string line in grid) { sb.AppendLine(line.Substring(0, line.Length - 1)); } return sb.ToString(); }
/// <summary> /// Adds a set of lines to the chart for test purposes /// </summary> /// <param name="xyLineChart"></param> public static void AddTestLines(XYLineChart xyLineChart) { // Add test Lines to demonstrate the control xyLineChart.Primitives.Clear(); double limit = 5; double increment = 1; // Create 3 normal lines ChartPrimitive[] lines = new ChartPrimitive[3]; //for (int lineNo = 0; lineNo < 3; ++lineNo) //{ // ChartPrimitive line = new ChartPrimitive(); // // Label the lines // line.Filled = true; // line.Dashed = false; // line.ShowInLegend = false; // line.AddPoint(0, 0); // // Draw 3 sine curves // for (double x = 0; x < limit + increment*.5; x += increment) // { // line.AddPoint(x, Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); // } // line.AddPoint(limit, 0); // // Add the lines to the chart // xyLineChart.Primitives.Add(line); // lines[lineNo] = line; //} //// Set the line colors to Red, Green, and Blue //lines[0].Color = Color.FromArgb(90,255,0,0); //lines[1].Color = Color.FromArgb(90, 0, 180, 0); //lines[2].Color = Color.FromArgb(90, 0, 0, 255); for (int lineNo = 0; lineNo < 3; ++lineNo) { ChartPrimitive line = new ChartPrimitive(); // Label the lines line.Label = "Test Line " + (lineNo + 1); //line.ShowInLegend = true; //line.HitTest = true; line.LineThickness = 1.5; // Draw 3 sine curves DateTime date = DateTime.Now; int i = 0; for (double x = 0; x < limit + increment*.5; x += increment) { line.AddPoint(date.Add(new TimeSpan(0, i++, 0)).Ticks, -2 * Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); } // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Colors.Red; lines[1].Color = Colors.Green; lines[2].Color = Colors.Blue; xyLineChart.Title = "Test Chart Title"; xyLineChart.XAxisLabel = "Test Chart X Axis"; xyLineChart.YAxisLabel = "Test Chart Y Axis"; xyLineChart.RedrawPlotLines(); }
/// <summary> /// Adds a set of lines to the chart for test purposes /// </summary> /// <param name="xyLineChart"></param> public static void AddTestLines(XYLineChart xyLineChart) { // Add test Lines to demonstrate the control xyLineChart.Primitives.Clear(); double limit = 5; double increment = 1; // Create 3 normal lines ChartPrimitive[] lines = new ChartPrimitive[3]; //for (int lineNo = 0; lineNo < 3; ++lineNo) //{ // ChartPrimitive line = new ChartPrimitive(); // // Label the lines // line.Filled = true; // line.Dashed = false; // line.ShowInLegend = false; // line.AddPoint(0, 0); // // Draw 3 sine curves // for (double x = 0; x < limit + increment*.5; x += increment) // { // line.AddPoint(x, Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); // } // line.AddPoint(limit, 0); // // Add the lines to the chart // xyLineChart.Primitives.Add(line); // lines[lineNo] = line; //} //// Set the line colors to Red, Green, and Blue //lines[0].Color = Color.FromArgb(90,255,0,0); //lines[1].Color = Color.FromArgb(90, 0, 180, 0); //lines[2].Color = Color.FromArgb(90, 0, 0, 255); for (int lineNo = 0; lineNo < 3; ++lineNo) { ChartPrimitive line = new ChartPrimitive(); // Label the lines line.Label = "Test Line " + (lineNo + 1); //line.ShowInLegend = true; //line.HitTest = true; line.LineThickness = 1.5; // Draw 3 sine curves DateTime date = DateTime.Now; int i = 0; for (double x = 0; x < limit + increment * .5; x += increment) { line.AddPoint(date.Add(new TimeSpan(0, i++, 0)).Ticks, -2 * Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); } // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Colors.Red; lines[1].Color = Colors.Green; lines[2].Color = Colors.Blue; xyLineChart.Title = "Test Chart Title"; xyLineChart.XAxisLabel = "Test Chart X Axis"; xyLineChart.YAxisLabel = "Test Chart Y Axis"; xyLineChart.RedrawPlotLines(); }