2017-03-03 353 views
0

NPOI 2.2.1版本随着NPOI的使用,如何在excel中绘制图表轴?

以下是用折线图生成excel的示例代码,但生成图表时没有坐标轴。

bottomAxis的属性(IsVisible)已为true,但仍无法看到它。

我的问题是如何使这些轴可见?

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 

    Dim wb As IWorkbook = New XSSFWorkbook() 
    Dim ws As ISheet = wb.createSheet("linechart") 
    Dim NUM_OF_ROWS = 3 
    Dim NUM_OF_COLUMNS = 10 


    For rowIndex As Integer = 0 To NUM_OF_ROWS 
     Dim row = ws.CreateRow(rowIndex + 1) 
     For colIndex As Integer = 0 To NUM_OF_COLUMNS 
      Dim cell = ws.GetRow(rowIndex + 1).CreateCell(colIndex) 
      cell.setCellValue(colIndex * (rowIndex + 1)) 
     Next 

    Next 
    Dim drawing = ws.createDrawingPatriarch() 
    Dim anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15) 
    Dim chart As IChart = drawing.createChart(anchor) 
    Dim legend = chart.getOrCreateLegend() 

    Dim dataFactory = chart.ChartDataFactory 
    Dim chartAxisFactory = chart.ChartAxisFactory 

    Dim lineChartData = dataFactory.createLineChartData(Of Double, Double)() 
    Dim bottomAxis = chartAxisFactory.createCategoryAxis(NPOI.SS.UserModel.Charts.AxisPosition.BOTTOM) 
    Dim leftAxis = chartAxisFactory.createValueAxis(NPOI.SS.UserModel.Charts.AxisPosition.RIGHT) 


    Dim xs = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)) 
    Dim ys1 = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)) 
    Dim ys2 = NPOI.SS.UserModel.charts.DataSources.fromNumericCellRange(ws, New NPOI.SS.Util.CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)) 


    lineChartData.addSeries(xs, ys1) 
    lineChartData.addSeries(xs, ys2) 

    chart.plot(lineChartData, bottomAxis, leftAxis) 

    Dim file As FileStream = New FileStream("D:/feed//Xiaohongshu/" & "test" & ".xlsx", FileMode.Create) 
    wb.Write(file) 
    file.Close() 


End Sub 

回答

0

这可能是NPOI 2.2.1 我们需要ISVISIBLE设置为False错误时deafult是真

bottomAxis.IsVisible = False 
leftAxis.IsVisible = False