/// <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;
            }
        }
Example #2
0
        public void TestStringArrayDataSource()
        {
            String[] strings = new String[] { "one", "two", "three", "four", "five" };
            IChartDataSource <String> stringDataSource = DataSources.FromArray(strings);

            Assert.IsFalse(stringDataSource.IsNumeric);
            Assert.IsFalse(stringDataSource.IsReference);
            AssertDataSourceIsEqualToArray(stringDataSource, strings);
        }
Example #3
0
        public void TestNumericArrayDataSource()
        {
            Double[] doubles = new Double[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
            IChartDataSource <Double> doubleDataSource = DataSources.FromArray(doubles);

            Assert.IsTrue(doubleDataSource.IsNumeric);
            Assert.IsFalse(doubleDataSource.IsReference);
            AssertDataSourceIsEqualToArray(doubleDataSource, doubles);
        }