/// <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;
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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();
        }