/// <summary> /// Initializes a new instance of the <see cref="PieChart"/> class. /// </summary> /// <param name="tooltip">The default tool tip control.</param> /// <param name="legend">The default legend.</param> public PieChart(IChartTooltip <SkiaSharpDrawingContext>?tooltip = null, IChartLegend <SkiaSharpDrawingContext>?legend = null) : base(tooltip, legend) { _seriesObserver = new CollectionDeepObserver <ISeries>( (object?sender, NotifyCollectionChangedEventArgs e) => { if (sender is IStopNPC stop && !stop.IsNotifyingChanges) { return; } OnPropertyChanged(); }, (object?sender, PropertyChangedEventArgs e) => { if (sender is IStopNPC stop && !stop.IsNotifyingChanges) { return; } OnPropertyChanged(); }, true); var c = Controls[0].Controls[0]; c.MouseDown += OnMouseDown; }
private static void CreateChart(ISheet sheet, IDrawing drawing, IClientAnchor anchor, string serieTitle, int startDataRow, int endDataRow, int columnIndex) { XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor); IBarChartData <string, double> barChartData = chart.ChartDataFactory.CreateBarChartData <string, double>(); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.Bottom; IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); bottomAxis.MajorTickMark = AxisTickMark.None; IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = AxisCrosses.AutoZero; leftAxis.SetCrossBetween(AxisCrossBetween.Between); IChartDataSource <string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0)); IChartDataSource <double> valueAxis = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnIndex, columnIndex)); var serie = barChartData.AddSeries(categoryAxis, valueAxis); serie.SetTitle(serieTitle); chart.Plot(barChartData, bottomAxis, leftAxis); }
static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2, bool enableMajorGridline = false) { XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor); chart.SetTitle("Test 1"); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.TopRight; ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>(); // Use a category axis for the bottom axis. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = AxisCrosses.AutoZero; IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); var s1 = data.AddSeries(xs, ys1); s1.SetTitle(serie1); var s2 = data.AddSeries(xs, ys2); s2.SetTitle(serie2); chart.Plot(data, bottomAxis, leftAxis); //add major gridline, available since NPOI 2.5.5 var plotArea = chart.GetCTChart().plotArea; plotArea.catAx[0].AddNewMajorGridlines(); plotArea.valAx[0].AddNewMajorGridlines(); }
static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2) { IChart chart = drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.TopRight; ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>(); // Use a category axis for the bottom axis. IChartAxis bottomAxis = chart.GetChartAxisFactory().CreateCategoryAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.GetChartAxisFactory().CreateValueAxis(AxisPosition.Left); leftAxis.SetCrosses(AxisCrosses.AutoZero); IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); var s1 = data.AddSeries(xs, ys1); s1.SetTitle(serie1); var s2 = data.AddSeries(xs, ys2); s2.SetTitle(serie2); chart.Plot(data, bottomAxis, leftAxis); }
public DrawSeriesPara(Grid chartGrid, Canvas chartCanvas, IEnumerable <ISeries> seriesCollection, LegendAddResult legendAddResult, IChartLegend legend) { this.ChartGrid = chartGrid; this.ChartCanvas = chartCanvas; this.SeriesCollection = seriesCollection; this.LegendAddResult = legendAddResult; this.Legend = legend; }
/// <summary> /// 创建一个图表实例 /// </summary> /// <param name="excelChart"></param> /// <param name="sheet"></param> private void CreateChart(NPOIExcelChart excelChart, ISheet sheet, int startRow, int endRow) { if (_excelType != NPOIExcelType.XLS) { throw new NotImplementedException("只支持.xls文件作图"); } IDrawing drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 0, startRow, excelChart.Axis.Count, endRow); XSSFChart chart = drawing.CreateChart(anchor) as XSSFChart; chart.Title.String = excelChart.Title; IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.TopRight; //坐标轴 var axis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); axis.IsVisible = true; //值轴 var data = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); data.IsVisible = true; data.Crosses = (AxisCrosses.AutoZero); IChartDataSource <string> xs = DataSources.FromArray <string>(excelChart.Axis.ToArray()); switch (excelChart.ExcelChartType) { case NPOIExcelChartType.Bar: var chartBarData = chart.ChartDataFactory.CreateBarChartData <string, double>(); foreach (var item in excelChart.Data) { var curData = DataSources.FromArray <double>(item.Value.ToArray()); var curSeries = chartBarData.AddSeries(xs, curData); curSeries.SetTitle(item.Key); } chart.Plot(chartBarData, axis, data); return; case NPOIExcelChartType.Line: var chartLineData = chart.ChartDataFactory.CreateLineChartData <string, double>(); foreach (var item in excelChart.Data) { var curData = DataSources.FromArray <double>(item.Value.ToArray()); var curSeries = chartLineData.AddSeries(xs, curData); curSeries.SetTitle(item.Key); } chart.Plot(chartLineData, axis, data); break; default: break; } }
private void FreezeAllLayout(ChartCollection <AxisAbs> axisCollection, IChartLegend legend, Canvas chartCanvas, Grid chartGrid, ScrollViewer scrollViewer, Grid chartContentGrid, LegendAddResult legendAddResult, AxisXHeightInfo axisXHeightInfo, AxisYWidthInfo axisYWidthInfo) { //添加scrollViewer scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; scrollViewer.VerticalAlignment = System.Windows.VerticalAlignment.Top; scrollViewer.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; scrollViewer.Content = chartContentGrid; chartGrid.Children.Add(scrollViewer); //布局legend var chartGridRowColumnDefinition = new ChartGridRowColumnDefinition(legendAddResult.HasLegend, legend, chartGrid); if (legendAddResult.HasLegend) { this.SetRowColumn(chartGrid, legend.LegendControl, chartGridRowColumnDefinition.Legend); } this.SetRowColumn(chartGrid, scrollViewer, chartGridRowColumnDefinition.Chart); //布局内部 var chartGridRowColumnDefinition2 = new ChartGridRowColumnDefinition(false, null, chartContentGrid, axisYWidthInfo, axisXHeightInfo); if (axisCollection != null && axisCollection.Count > ChartConstant.ZERO_I) { RowColumnDefinitionItem rowColumnDefinition; foreach (var axis in axisCollection) { switch (axis.DockOrientation) { case ChartDockOrientation.Left: rowColumnDefinition = chartGridRowColumnDefinition2.LeftAxis; break; case ChartDockOrientation.Top: rowColumnDefinition = chartGridRowColumnDefinition2.TopAxis; break; case ChartDockOrientation.Right: rowColumnDefinition = chartGridRowColumnDefinition2.RightAxis; break; case ChartDockOrientation.Bottom: rowColumnDefinition = chartGridRowColumnDefinition2.BottomAxis; break; default: throw new NotImplementedException(axis.DockOrientation.ToString()); } this.SetRowColumn(chartContentGrid, axis.AxisControl, rowColumnDefinition); } } this.SetRowColumn(chartContentGrid, chartCanvas, chartGridRowColumnDefinition2.Chart); }
static void Main(string[] args) { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet("linechart"); int NUM_OF_ROWS = 3; int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. IRow row; ICell cell; for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { row = sheet.CreateRow((short)rowIndex); for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { cell = row.CreateCell((short)colIndex); cell.SetCellValue(colIndex * (rowIndex + 1)); } } IDrawing drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15); IChart chart = drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.TopRight; ILineChartData <double, double> data = chart.GetChartDataFactory().CreateLineChartData <double, double>(); // Use a category axis for the bottom axis. IChartAxis bottomAxis = chart.GetChartAxisFactory().CreateCategoryAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.GetChartAxisFactory().CreateValueAxis(AxisPosition.Left); leftAxis.SetCrosses(AxisCrosses.AutoZero); IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); var s1 = data.AddSerie(xs, ys1); s1.SetTitle("title1"); var s2 = data.AddSerie(xs, ys2); s2.SetTitle("title2"); chart.Plot(data, bottomAxis, leftAxis); using (FileStream fs = File.Create("test.xlsx")) { wb.Write(fs); } }
/// <summary> /// FreezeX /// </summary> /// <param name="hasLegend"></param> /// <param name="legend"></param> /// <param name="chartGrid"></param> /// <param name="axisXHeightInfo"></param> public ChartGridRowColumnDefinition(bool hasLegend, IChartLegend legend, Grid chartGrid, AxisXHeightInfo axisXHeightInfo) { if (hasLegend) { this.Legend = new RowColumnDefinitionItem(); } if (axisXHeightInfo.TopAxisTotalHeight > ChartConstant.ZERO_D) { this.TopAxis = new RowColumnDefinitionItem(); } if (axisXHeightInfo.BottomAxisTotalHeight > ChartConstant.ZERO_D) { this.BottomAxis = new RowColumnDefinitionItem(); } this.Chart = new RowColumnDefinitionItem(); if (axisXHeightInfo.TopAxisTotalHeight > ChartConstant.ZERO_D) { this.TopAxis.CreateRow(chartGrid, axisXHeightInfo.TopAxisTotalHeight, GridUnitType.Pixel); } this.Chart.CreateRow(chartGrid, ChartConstant.GRID_START_SIZE, GridUnitType.Star); if (axisXHeightInfo.BottomAxisTotalHeight > ChartConstant.ZERO_D) { this.BottomAxis.CreateRow(chartGrid, axisXHeightInfo.BottomAxisTotalHeight, GridUnitType.Pixel); } if (hasLegend) { if (legend.DockOrientation == ChartDockOrientation.Left || legend.DockOrientation == ChartDockOrientation.Right) { this.Chart.CreateColumn(chartGrid, ChartConstant.GRID_START_SIZE, GridUnitType.Star); } this.Legend.CreateLegendRowColumn(chartGrid, legend); } if (this.TopAxis != null) { this.TopAxis.MergeRowColumn(chartGrid, this.Chart); } if (this.BottomAxis != null) { this.BottomAxis.MergeRowColumn(chartGrid, this.Chart); } this.Chart.MergeRowColumn(chartGrid, this.Chart); }
public void GenaretionChart() { FileStream RfileStream = new FileStream("D:\\test.xlsx", FileMode.Open, FileAccess.Read); //建立讀取資料的FileStream XSSFWorkbook wb = new XSSFWorkbook(RfileStream); //讀取檔案內的Workbook物件 ISheet Wsheet = wb.GetSheetAt(1); //選擇圖表存放的sheet ISheet Rsheet = wb.GetSheetAt(0); //選擇資料來源的sheet IDrawing drawing = Wsheet.CreateDrawingPatriarch(); //sheet產生drawing物件 IClientAnchor clientAnchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 0, 5, 10); //設定圖表位置 IChart chart = drawing.CreateChart(clientAnchor); //產生chart物件 IChartLegend legend = chart.GetOrCreateLegend(); //還沒研究出這行在做甚麼 legend.Position = LegendPosition.TopRight; ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>(); //產生存放資料的物件(資料型態為double) IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); //設定X軸 IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); //設定Y軸 bottomAxis.Crosses = AxisCrosses.AutoZero; //設定X軸數值開始為0 leftAxis.Crosses = AxisCrosses.AutoZero; //設定Y軸數值開始為0 IChartDataSource <double> xs = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 0, 0)); //取得要讀取sheet的資料位置(CellRangeAddress(first_row,end_row, first_column, end_column)) //x軸資料 IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 1, 1)); //第一條y軸資料 IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 2, 2)); //第二條y軸資料 data.AddSeries(xs, ys1); data.AddSeries(xs, ys2); //加入到data chart.Plot(data, bottomAxis, leftAxis); //加入到chart FileStream WfileStream = new FileStream("D:\\test.xlsx", FileMode.Create, FileAccess.Write); //建立寫入資料的FileStream wb.Write(WfileStream); //將workbook寫入資料 RfileStream.Close(); //關閉FileStream WfileStream.Close(); //關閉FileStream }
private IManualLayout GetEmptyLayout() { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet(); IDrawing Drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30); IChart chart = Drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); return(legend.GetManualLayout()); }
public void TestLegendPositionAccessMethods() { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet(); IDrawing Drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30); IChart chart = Drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.TopRight; Assert.AreEqual(LegendPosition.TopRight, legend.Position); }
private void UpdateAll() { if (this.IgnoreUpdateChart()) { return; } AxisFreezeInfo axisFreezeInfo = this.GetAxisFreezeInfo(); ChartCollection <AxisAbs> axes = this.Axes; ChartCollection <ISeries> series = this.Series; IChartLegend legend = this.Legend; Grid chartGrid = this._chartGrid; Canvas chartCanvas = this._chartCanvas; Grid chartContentGrid = this._chartContentGrid; ScrollViewer scrollViewer = this._scrollViewer; this.Content = null; scrollViewer.Content = null; this._scrollViewer.Content = null; chartGrid.Children.Clear(); chartCanvas.Children.Clear(); chartCanvas.MouseWheel -= ChartCanvas_MouseWheel; chartCanvas.MouseLeftButtonDown -= ChartCanvas_MouseLeftButtonDown; chartCanvas.MouseMove -= ChartCanvas_MouseMove; chartGrid.RowDefinitions.Clear(); chartGrid.ColumnDefinitions.Clear(); chartContentGrid.Children.Clear(); chartContentGrid.RowDefinitions.Clear(); chartContentGrid.ColumnDefinitions.Clear(); switch (axisFreezeInfo.AxisFreezeType) { case AxisFreezeType.None: this.UpdateNoneFreeze(axisFreezeInfo, axes, series, legend, chartCanvas, chartGrid, scrollViewer); break; case AxisFreezeType.X: this.UpdateFreezeX(axisFreezeInfo, axes, series, legend, chartCanvas, chartGrid, scrollViewer, chartContentGrid); break; case AxisFreezeType.Y: this.UpdateFreezeY(axisFreezeInfo, axes, series, legend, chartCanvas, chartGrid, scrollViewer, chartContentGrid); break; case AxisFreezeType.All: this.UpdateFreezeAll(axisFreezeInfo, axes, series, legend, chartCanvas, chartGrid, scrollViewer, chartContentGrid); break; default: throw new NotImplementedException(axisFreezeInfo.AxisFreezeType.ToString()); } }
internal void CreateLegendRowColumn(Grid chartGrid, IChartLegend legend) { double legendSize = legend.Size; switch (legend.DockOrientation) { case ChartDockOrientation.Left: case ChartDockOrientation.Right: this._column = new ColumnDefinition() { Width = new GridLength(legend.Size, GridUnitType.Pixel) }; if (legend.DockOrientation == ChartDockOrientation.Left) { chartGrid.ColumnDefinitions.Insert(0, this._column); this.ColumnIndex = 0; } else { chartGrid.ColumnDefinitions.Add(this._column); this.ColumnIndex = chartGrid.ColumnDefinitions.Count - 1; } this.RowIndex = 0; this.RowSpan = chartGrid.RowDefinitions.Count; break; case ChartDockOrientation.Top: case ChartDockOrientation.Bottom: this._row = new RowDefinition() { Height = new GridLength(legend.Size, GridUnitType.Pixel) }; if (legend.DockOrientation == ChartDockOrientation.Top) { chartGrid.RowDefinitions.Insert(0, this._row); this.RowIndex = 0; } else { chartGrid.RowDefinitions.Add(this._row); this.RowIndex = chartGrid.RowDefinitions.Count - 1; } this.ColumnIndex = 0; this.ColumnSpan = chartGrid.ColumnDefinitions.Count; break; default: throw new NotImplementedException(legend.DockOrientation.ToString()); } }
public void Main() { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet("Sheet 1"); int NUM_OF_ROWS = 3; int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. IRow row; ICell cell; for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { row = sheet.CreateRow((short)rowIndex); for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { cell = row.CreateCell((short)colIndex); cell.SetCellValue(colIndex * (rowIndex + 1)); } } IDrawing drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15); IChart chart = drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = (LegendPosition.TopRight); IScatterChartData <double, double> data = chart.ChartDataFactory.CreateScatterChartData <double, double>(); IValueAxis bottomAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = AxisCrosses.AutoZero; IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.AddSeries(xs, ys1); data.AddSeries(xs, ys2); chart.Plot(data, bottomAxis, leftAxis); // Write the output to a file FileStream sw = File.Create("test.xlsx"); wb.Write(sw); sw.Close(); }
private void DrawSeries(Grid chartGrid, Canvas chartCanvas, IEnumerable <ISeries> seriesCollection, LegendAddResult legendAddResult, IChartLegend legend) { //第七步 绘各Series this.DrawSeries(chartCanvas, seriesCollection); //填充legend if (legendAddResult != null && legendAddResult.HasLegend && legend != null) { this.UpdateLegend(legend, seriesCollection); } }
private void UpdateLegend(IChartLegend legend, IEnumerable <ISeries> seriesCollection) { var legendBrushList = new List <SeriesLegendItem>(); if (seriesCollection != null) { foreach (ISeries series in seriesCollection) { series.AppendLegendItemToList(legendBrushList); } } legend.UpdateLegend(legendBrushList); }
public void Test_setOverlay_defaultChartLegend_expectOverlayInitialValueSetToFalse() { // Arrange IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet(); IDrawing Drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30); IChart chart = Drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); // Act // Assert Assert.IsFalse(legend.IsOverlay); }
private IWorkbook CreateWorkbookWithChart() { IWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet("linechart"); int NUM_OF_ROWS = 3; int NUM_OF_COLUMNS = 10; // Create a row and Put some cells in it. Rows are 0 based. IRow row; ICell cell; for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { row = sheet.CreateRow((short)rowIndex); for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { cell = row.CreateCell((short)colIndex); cell.SetCellValue(colIndex * (rowIndex + 1)); } } IDrawing Drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = Drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15); IChart chart = Drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = (/*setter*/ LegendPosition.TopRight); ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>(); // Use a category axis for the bottom axis. IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = (/*setter*/ AxisCrosses.AutoZero); IChartDataSource <double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.AddSeries(xs, ys1); data.AddSeries(xs, ys2); chart.Plot(data, bottomAxis, leftAxis); return(wb); }
private void NoneFreezeLayout(ChartCollection <AxisAbs> axisCollection, IChartLegend legend, Canvas chartCanvas, Grid chartGrid, ScrollViewer scrollViewer, LegendAddResult legendAddResult, AxisXHeightInfo axisXHeightInfo, AxisYWidthInfo axisYWidthInfo) { var chartGridRowColumnDefinition = new ChartGridRowColumnDefinition(legendAddResult.HasLegend, legend, chartGrid, axisYWidthInfo, axisXHeightInfo); if (legendAddResult.HasLegend) { this.SetRowColumn(chartGrid, legend.LegendControl, chartGridRowColumnDefinition.Legend); } if (axisCollection != null && axisCollection.Count > ChartConstant.ZERO_I) { RowColumnDefinitionItem rowColumnDefinition; foreach (var axis in axisCollection) { switch (axis.DockOrientation) { case ChartDockOrientation.Left: rowColumnDefinition = chartGridRowColumnDefinition.LeftAxis; break; case ChartDockOrientation.Top: rowColumnDefinition = chartGridRowColumnDefinition.TopAxis; break; case ChartDockOrientation.Right: rowColumnDefinition = chartGridRowColumnDefinition.RightAxis; break; case ChartDockOrientation.Bottom: rowColumnDefinition = chartGridRowColumnDefinition.BottomAxis; break; default: throw new NotImplementedException(axis.DockOrientation.ToString()); } this.SetRowColumn(chartGrid, axis.AxisControl, rowColumnDefinition); } } this.SetRowColumn(chartGrid, scrollViewer, chartGridRowColumnDefinition.Chart); chartCanvas.MouseWheel += ChartCanvas_MouseWheel; chartCanvas.MouseLeftButtonDown += ChartCanvas_MouseLeftButtonDown; chartCanvas.MouseMove += ChartCanvas_MouseMove; }
public override void OnApplyTemplate() { base.OnApplyTemplate(); if (!(Template.FindName("canvas", this) is NaturalGeometriesCanvas canvas)) { throw new Exception( $"{nameof(SKElement)} not found. This was probably caused because the control {nameof(CartesianChart)} template was overridden, " + $"If you override the template please add an {nameof(NaturalGeometriesCanvas)} to the template and name it 'canvas'"); } this.canvas = canvas; core = new ChartCore <SkiaDrawingContext>(this, canvas.CanvasCore); legend = Template.FindName("legend", this) as IChartLegend <SkiaDrawingContext>; tooltip = Template.FindName("tooltip", this) as IChartTooltip <SkiaDrawingContext>; core.Update(); }
private LegendAddResult AddLegend(IChartLegend legend, Grid chartGrid) { double left = 0d, top = 0d, right = 0d, bottom = 0d; double legendSize = double.NaN; bool hasLegend = false; if (legend != null) { FrameworkElement legendControl = legend.LegendControl; if (legendControl != null) { legendSize = legend.Size; if (legendSize > ChartConstant.ZERO_D) { chartGrid.Children.Add(legendControl);//使用Grid包装,主是为了居中对齐好布局,Canvas中水平垂直方向太麻烦 hasLegend = true; switch (legend.DockOrientation) { case ChartDockOrientation.Left: left = legendSize; break; case ChartDockOrientation.Top: top = legendSize; break; case ChartDockOrientation.Right: right = legendSize; break; case ChartDockOrientation.Bottom: bottom = legendSize; break; default: throw new NotImplementedException(legend.DockOrientation.ToString()); } } } } return(new LegendAddResult(hasLegend, left, top, right, bottom)); }
static void CreateChart(ISheet sheet) { XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch(); XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 3, 0, 10, 15); IChart chart = drawing.CreateChart(anchor); IChartLegend legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.Bottom; IBarChartData <string, double> data = chart.ChartDataFactory.CreateBarChartData <string, double>(); IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); bottomAxis.MajorTickMark = AxisTickMark.None; IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = AxisCrosses.AutoZero; leftAxis.SetCrossBetween(AxisCrossBetween.Between); IChartDataSource <string> categories = DataSources.FromStringCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 0, 0)); // Abono IChartDataSource <double> xAbono = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 1, 1)); // Isento IChartDataSource <double> xIsento = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 2, 2)); // Violação IChartDataSource <double> xViolacao = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 3, 3)); var s1 = data.AddSeries(categories, xAbono); s1.SetTitle("Abono"); var s2 = data.AddSeries(categories, xIsento); s2.SetTitle("Isento"); var s3 = data.AddSeries(categories, xViolacao); s3.SetTitle("Violação"); chart.Plot(data, bottomAxis, leftAxis); }
/// <summary> /// FreezeY /// </summary> /// <param name="hasLegend"></param> /// <param name="legend"></param> /// <param name="chartGrid"></param> public ChartGridRowColumnDefinition(bool hasLegend, IChartLegend legend, Grid chartGrid) { if (hasLegend) { this.Legend = new RowColumnDefinitionItem(); } this.Chart = new RowColumnDefinitionItem(); this.Chart.CreateColumn(chartGrid, ChartConstant.GRID_START_SIZE, GridUnitType.Star); this.Chart.CreateRow(chartGrid, ChartConstant.GRID_START_SIZE, GridUnitType.Star); if (hasLegend) { this.Legend.CreateLegendRowColumn(chartGrid, legend); } this.Chart.MergeRowColumn(chartGrid, this.Chart); }
private void UpdateThrottlerUnlocked() { // before measure every element in the chart // we copy the properties that might change while we are updating the chart // this call should be thread safe // ToDo: ensure it is thread safe... viewDrawMargin = chartView.DrawMargin; controlSize = chartView.ControlSize; yAxes = chartView.YAxes.Select(x => x.Copy()).ToArray(); xAxes = chartView.XAxes.Select(x => x.Copy()).ToArray(); measureWorker = new object(); series = chartView.Series.Select(series => { // a good implementation of ISeries<T> // must use the measureWorker to identify // if the points are already fetched. // this way no matter if the Series.Values collection changes // the fetch method will always return the same collection for the // current measureWorker instance series.Fetch(this); return(series); }).ToArray(); legendPosition = chartView.LegendPosition; legendOrientation = chartView.LegendOrientation; legend = chartView.Legend; // ... this is a reference type.. this has no sense tooltipPosition = chartView.TooltipPosition; tooltipFindingStrategy = chartView.TooltipFindingStrategy; tooltip = chartView.Tooltip; //... no sense again... animationsSpeed = chartView.AnimationsSpeed; easingFunction = chartView.EasingFunction; Measure(); }
/// <summary> /// Initializes a new instance of the <see cref="PieChart"/> class. /// </summary> /// <param name="tooltip">The default tool tip control.</param> /// <param name="legend">The default legend.</param> public PieChart(IChartTooltip <SkiaSharpDrawingContext>?tooltip = null, IChartLegend <SkiaSharpDrawingContext>?legend = null) : base(tooltip, legend) { _seriesObserver = new CollectionDeepObserver <ISeries>( (object?sender, NotifyCollectionChangedEventArgs e) => { if (core == null) { return; } core.Update(); }, (object?sender, PropertyChangedEventArgs e) => { if (core == null) { return; } core.Update(); }, true); }
/// <summary> /// Initializes a new instance of the <see cref="PolarChart"/> class. /// </summary> /// <param name="tooltip">The default tool tip control.</param> /// <param name="legend">The default legend control.</param> public PolarChart(IChartTooltip <SkiaSharpDrawingContext>?tooltip = null, IChartLegend <SkiaSharpDrawingContext>?legend = null) : base(tooltip, legend) { _seriesObserver = new CollectionDeepObserver <ISeries>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); _angleObserver = new CollectionDeepObserver <IPolarAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); _radiusObserver = new CollectionDeepObserver <IPolarAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); AngleAxes = new List <IPolarAxis>() { LiveCharts.CurrentSettings.PolarAxisProvider() }; RadiusAxes = new List <IPolarAxis>() { LiveCharts.CurrentSettings.PolarAxisProvider() }; Series = new ObservableCollection <ISeries>(); var c = Controls[0].Controls[0]; c.MouseWheel += OnMouseWheel; c.MouseDown += OnMouseDown; c.MouseUp += OnMouseUp; }
/// <summary> /// Initializes a new instance of the <see cref="CartesianChart"/> class. /// </summary> /// <param name="tooltip">The default tool tip control.</param> /// <param name="legend">The default legend control.</param> public CartesianChart(IChartTooltip <SkiaSharpDrawingContext>?tooltip = null, IChartLegend <SkiaSharpDrawingContext>?legend = null) : base(tooltip, legend) { _seriesObserver = new CollectionDeepObserver <ISeries>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); _xObserver = new CollectionDeepObserver <ICartesianAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); _yObserver = new CollectionDeepObserver <ICartesianAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); _sectionsObserverer = new CollectionDeepObserver <Section <SkiaSharpDrawingContext> >( OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); XAxes = new List <ICartesianAxis>() { LiveCharts.CurrentSettings.AxisProvider() }; YAxes = new List <ICartesianAxis>() { LiveCharts.CurrentSettings.AxisProvider() }; Series = new ObservableCollection <ISeries>(); var c = Controls[0].Controls[0]; c.MouseWheel += OnMouseWheel; c.MouseDown += OnMouseDown; c.MouseUp += OnMouseUp; }
/// <summary> /// Initializes a new instance of the <see cref="CartesianChart"/> class. /// </summary> /// <param name="tooltip">The default tool tip control.</param> /// <param name="legend">The default legend control.</param> public CartesianChart(IChartTooltip <SkiaSharpDrawingContext>?tooltip = null, IChartLegend <SkiaSharpDrawingContext>?legend = null) : base(tooltip, legend) { seriesObserver = new CollectionDeepObserver <ISeries>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); xObserver = new CollectionDeepObserver <IAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); yObserver = new CollectionDeepObserver <IAxis>(OnDeepCollectionChanged, OnDeepCollectionPropertyChanged, true); XAxes = new List <IAxis>() { new Axis() }; YAxes = new List <IAxis>() { new Axis() }; Series = new ObservableCollection <ISeries>(); var c = Controls[0].Controls[0]; c.MouseWheel += OnMouseWheel; c.MouseDown += OnMouseDown; c.MouseMove += OnMoseMove; c.MouseUp += OnMouseUp; panningThrottler = new ActionThrottler(DoPan, TimeSpan.FromMilliseconds(30)); }
private void UpdateFreezeY(AxisFreezeInfo axisFreezeInfo, ChartCollection <AxisAbs> axisCollection, ChartCollection <ISeries> seriesCollection, IChartLegend legend, Canvas chartCanvas, Grid chartGrid, ScrollViewer scrollViewer, Grid chartContentGrid) { /************************************************************************************************************ * 步骤: * 1.添加legend,并计算出发四周所占高度或宽度 * 2.计算X轴总高度 * 3.根据X轴总高度计算图表区域高度高度(等于Y轴高度) * 4.根据Y轴高度绘制Y轴,并计算Y轴宽度 * 5.根据宽度绘制X轴 * 6.绘制坐标背景标记线 * 7.布局UI * 8.绘各Series和填充legend ************************************************************************************************************/ //chartGrid.ShowGridLines = true; //chartCanvas.Background = ColorBrushHelper.GetNextColor(); this.Content = chartGrid; //第一步 添加legend,并计算出发四周所占高度或宽度 LegendAddResult legendAddResult = this.AddLegend(legend, chartGrid); //第二步 计算X轴总高度 AxisXHeightInfo axisXHeightInfo = this.CalculateAxisXHeight(axisCollection); //第三步 根据X轴总高度计算图表区域高度高度(等于Y轴高度) double yAxisHeight = axisFreezeInfo.Height - axisXHeightInfo.TopAxisTotalHeight - axisXHeightInfo.BottomAxisTotalHeight - legendAddResult.Top - legendAddResult.Bottom - this._scrollBarWidth; if (yAxisHeight < ChartConstant.ZERO_D) { yAxisHeight = ChartConstant.ZERO_D; } //第四步 根据Y轴高度绘制Y轴,并计算Y轴宽度 AxisYWidthInfo axisYWidthInfo = this.DrawAxisYByAxisXHeightInfo(axisCollection, chartGrid.Children, seriesCollection, yAxisHeight, axisXHeightInfo.TopAxisTotalHeight); //第五步 根据宽度绘制X轴 double width = this.ActualWidth - axisYWidthInfo.LeftAxisTotalWidth - axisYWidthInfo.RightAxisTotalWidth - legendAddResult.Left - legendAddResult.Right; double xAxisWidth = axisFreezeInfo.Width; if (xAxisWidth < ChartConstant.ZERO_D) { xAxisWidth = ChartConstant.ZERO_D; } else if (xAxisWidth - width < ChartConstant.ZERO_D) { //真实宽度大于最小值,取更大值 xAxisWidth = width; } Dictionary <AxisAbs, List <double> > axisXLabelDic = this.DrawAxisX(axisCollection, seriesCollection, chartContentGrid, xAxisWidth, ChartConstant.ZERO_D); chartCanvas.Width = xAxisWidth; chartCanvas.Height = yAxisHeight; chartContentGrid.Children.Add(chartCanvas); //第六步 绘制坐标背景标记线 this.DrawAxisBackgroundLabelLine(chartCanvas, axisYWidthInfo.AxisYLabelDic, axisXLabelDic); //第七步 布局UI this.FreezeYLayout(axisCollection, legend, chartCanvas, chartGrid, scrollViewer, chartContentGrid, legendAddResult, axisXHeightInfo, axisYWidthInfo); //第八步 绘各Series和填充legend this.DrawSeries(chartGrid, chartCanvas, seriesCollection, legendAddResult, legend); }