Primary and Second Axis
Possible Usage Scenarios
When the numbers in a chart vary widely from data series to data series, or when you have mixed types of data (price and volume), plot one or more data series on a secondary vertical (value) axis. The scale of the secondary vertical axis shows the values for the associated data series. A secondary axis works well in a chart that shows a combination of column and line charts.
Handle Primary and Second Axis like Microsoft Excel
Please see the following sample code that create a new Excel file and put values of the chart in the first worksheet. Then we add a chart and show the second-axis.
Sample Code
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType | |
from aspose.cells.drawing import FillType | |
# Create an instance of Workbook | |
workbook = Workbook() | |
# Access the first worksheet. | |
worksheet = workbook.worksheets[0] | |
# Put the sample values used in a chart | |
worksheet.cells.get("A1").put_value("Region") | |
worksheet.cells.get("A2").put_value("Peking") | |
worksheet.cells.get("A3").put_value("New York") | |
worksheet.cells.get("A4").put_value("Paris") | |
worksheet.cells.get("B1").put_value("Sales Volume") | |
worksheet.cells.get("C1").put_value("Growth Rate") | |
worksheet.cells.get("B2").put_value(100) | |
worksheet.cells.get("B3").put_value(80) | |
worksheet.cells.get("B4").put_value(140) | |
worksheet.cells.get("C2").put_value(0.7) | |
worksheet.cells.get("C3").put_value(0.8) | |
worksheet.cells.get("C4").put_value(1.0) | |
# Create a Scatter chart | |
pieIdx = worksheet.charts.add(ChartType.SCATTER, 6, 6, 15, 11) | |
# Retrieve the Chart object | |
chart = worksheet.charts[pieIdx] | |
# Add Series | |
chart.n_series.add("B2:C4", True) | |
# Set the category data | |
chart.n_series.category_data = "=Sheet1!$A$2:$A$4" | |
# Set the Second-Axis | |
chart.n_series[1].plot_on_second_axis = True | |
# Show the Second-Axis | |
chart.second_value_axis.is_visible = True | |
# Set the second series ChartType to line | |
chart.n_series[1].type = ChartType.LINE | |
# Set the series name | |
chart.n_series[0].name = "Sales Volume" | |
chart.n_series[1].name = "Growth Rate" | |
# Set the Legend at the bottom of the chart area | |
chart.legend.position = LegendPositionType.BOTTOM | |
# Fill the PlotArea area with nothing | |
chart.plot_area.area.fill_format.fill_type = FillType.NONE | |
# Save the file | |
workbook.save("PrimaryandSecondaryAxis.xlsx") |