|
| 1 | +# https://github.com/jmcnamara/libxlsxwriter/blob/main/examples/chart_scatter.c |
| 2 | + |
| 3 | +using LibXLSXWriter: worksheet_write_string, CELL, worksheet_write_number, workbook_new, workbook_add_worksheet, workbook_add_format, format_set_bold, workbook_add_chart, LXW_CHART_SCATTER, chart_add_series, chart_series_set_name, chart_series_set_categories, chart_series_set_values, chart_series_set_name_range, chart_title_set_name, chart_axis_set_name, chart_set_style, worksheet_insert_chart, lxw_worksheet, lxw_format, chart_axis_get, LXW_CHART_AXIS_TYPE_X, LXW_CHART_AXIS_TYPE_Y, LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS, LXW_CHART_SCATTER_STRAIGHT, LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS, LXW_CHART_SCATTER_SMOOTH, workbook_close |
| 4 | + |
| 5 | +# Write some data to the worksheet. |
| 6 | +function write_worksheet_data(worksheet::Ptr{lxw_worksheet}, bold::Ptr{lxw_format}) |
| 7 | + data = [ |
| 8 | + 2 10 30 |
| 9 | + 3 40 60 |
| 10 | + 4 50 70 |
| 11 | + 5 20 50 |
| 12 | + 6 10 40 |
| 13 | + 7 50 30 |
| 14 | + ] |
| 15 | + |
| 16 | + worksheet_write_string(worksheet, CELL("A1")..., "Number", bold) |
| 17 | + worksheet_write_string(worksheet, CELL("B1")..., "Batch 1", bold) |
| 18 | + worksheet_write_string(worksheet, CELL("C1")..., "Batch 2", bold) |
| 19 | + |
| 20 | + for row in 0:5, col in 0:2 |
| 21 | + worksheet_write_number(worksheet, row + 1, col, data[row+1,col+1] , C_NULL) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +### Create a worksheet with examples charts. |
| 26 | +workbook = workbook_new("chart_scatter.xlsx") |
| 27 | +worksheet = workbook_add_worksheet(workbook, C_NULL) |
| 28 | +# Add a bold format to use to highlight the header cells. |
| 29 | +bold = workbook_add_format(workbook) |
| 30 | +format_set_bold(bold) |
| 31 | + |
| 32 | +# Write some data for the chart. |
| 33 | +write_worksheet_data(worksheet, bold) |
| 34 | + |
| 35 | + |
| 36 | +### Chart 1. Create a scatter chart, the default shows points only. |
| 37 | + |
| 38 | +chart = workbook_add_chart(workbook, LXW_CHART_SCATTER) |
| 39 | + |
| 40 | +# Add the first series to the chart. |
| 41 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$B$2:$B$7") |
| 42 | + |
| 43 | +# Set the name for the series instead of the default "Series 1". |
| 44 | +chart_series_set_name(series, raw"=Sheet1!$B$1") |
| 45 | + |
| 46 | +# Add a second series but leave the categories and values undefined. They can be defined later using the alternative syntax shown below. |
| 47 | +series = chart_add_series(chart, C_NULL, C_NULL) |
| 48 | + |
| 49 | +# Configure the series using a syntax that is easier to define programmatically. |
| 50 | +chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0) # "=Sheet1!$A$2:$A$7" |
| 51 | +chart_series_set_values(series, "Sheet1", 1, 2, 6, 2) # "=Sheet1!$C$2:$C$7" |
| 52 | +chart_series_set_name_range(series, "Sheet1", 0, 2) # "=Sheet1!$C$1" |
| 53 | + |
| 54 | +# Add a chart title and some axis labels. |
| 55 | +chart_title_set_name(chart, "Results of sample analysis") |
| 56 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_X), "Test number") |
| 57 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_Y), "Sample length (mm)") |
| 58 | + |
| 59 | +# Set an Excel chart style. |
| 60 | +chart_set_style(chart, 11) |
| 61 | + |
| 62 | +# Insert the chart into the worksheet. |
| 63 | +worksheet_insert_chart(worksheet, CELL("E2")..., chart) |
| 64 | + |
| 65 | + |
| 66 | +### Chart 2. Create a scatter chart with straight lines and markers connecting the points. |
| 67 | + |
| 68 | +chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS) |
| 69 | + |
| 70 | +# Add the first series to the chart. |
| 71 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$B$2:$B$7") |
| 72 | + |
| 73 | +# Set the name for the series instead of the default "Series 1". |
| 74 | +chart_series_set_name(series, raw"=Sheet1!$B$1") |
| 75 | + |
| 76 | +# Add the second series to the chart. |
| 77 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$C$2:$C$7") |
| 78 | + |
| 79 | +# Set the name for the series instead of the default "Series 2". |
| 80 | +chart_series_set_name(series, raw"=Sheet1!$C$1") |
| 81 | + |
| 82 | +# Add a chart title and some axis labels. |
| 83 | +chart_title_set_name(chart, "Results of sample analysis") |
| 84 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_X), "Test number") |
| 85 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_Y), "Sample length (mm)") |
| 86 | + |
| 87 | +# Set an Excel chart style. |
| 88 | +chart_set_style(chart, 12) |
| 89 | + |
| 90 | +# Insert the chart into the worksheet. |
| 91 | +worksheet_insert_chart(worksheet, CELL("E18")..., chart) |
| 92 | + |
| 93 | + |
| 94 | +### Chart 3. Create a scatter chart with straight lines connecting the points. |
| 95 | + |
| 96 | +chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_STRAIGHT) |
| 97 | + |
| 98 | +# Add the first series to the chart. |
| 99 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$B$2:$B$7") |
| 100 | + |
| 101 | +# Set the name for the series instead of the default "Series 1". |
| 102 | +chart_series_set_name(series, raw"=Sheet1!$B$1") |
| 103 | + |
| 104 | +# Add the second series to the chart. |
| 105 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$C$2:$C$7") |
| 106 | + |
| 107 | +# Set the name for the series instead of the default "Series 2". |
| 108 | +chart_series_set_name(series, raw"=Sheet1!$C$1") |
| 109 | + |
| 110 | +# Add a chart title and some axis labels. |
| 111 | +chart_title_set_name(chart, "Results of sample analysis") |
| 112 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_X), "Test number") |
| 113 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_Y), "Sample length (mm)") |
| 114 | + |
| 115 | +# Set an Excel chart style. |
| 116 | +chart_set_style(chart, 13) |
| 117 | + |
| 118 | +# Insert the chart into the worksheet. |
| 119 | +worksheet_insert_chart(worksheet, CELL("E34")..., chart) |
| 120 | + |
| 121 | + |
| 122 | +### Chart 4. Create a scatter chart with smooth lines and markers connecting the points. |
| 123 | + |
| 124 | +chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS) |
| 125 | + |
| 126 | +# Add the first series to the chart. |
| 127 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$B$2:$B$7") |
| 128 | + |
| 129 | +# Set the name for the series instead of the default "Series 1". |
| 130 | +chart_series_set_name(series, raw"=Sheet1!$B$1") |
| 131 | + |
| 132 | +# Add the second series to the chart. |
| 133 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$C$2:$C$7") |
| 134 | + |
| 135 | +# Set the name for the series instead of the default "Series 2". |
| 136 | +chart_series_set_name(series, raw"=Sheet1!$C$1") |
| 137 | + |
| 138 | +# Add a chart title and some axis labels. |
| 139 | +chart_title_set_name(chart, "Results of sample analysis") |
| 140 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_X), "Test number") |
| 141 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_Y), "Sample length (mm)") |
| 142 | + |
| 143 | +# Set an Excel chart style. |
| 144 | +chart_set_style(chart, 14) |
| 145 | + |
| 146 | +# Insert the chart into the worksheet. |
| 147 | +worksheet_insert_chart(worksheet, CELL("E50")..., chart) |
| 148 | + |
| 149 | + |
| 150 | +### Chart 5. Create a scatter chart with smooth lines connecting the points. |
| 151 | + |
| 152 | +chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_SMOOTH) |
| 153 | + |
| 154 | +# Add the first series to the chart. |
| 155 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$B$2:$B$7") |
| 156 | + |
| 157 | +# Set the name for the series instead of the default "Series 1". |
| 158 | +chart_series_set_name(series, raw"=Sheet1!$B$1") |
| 159 | + |
| 160 | +# Add the second series to the chart. |
| 161 | +series = chart_add_series(chart, raw"=Sheet1!$A$2:$A$7", raw"=Sheet1!$C$2:$C$7") |
| 162 | + |
| 163 | +# Set the name for the series instead of the default "Series 2". |
| 164 | +chart_series_set_name(series, raw"=Sheet1!$C$1") |
| 165 | + |
| 166 | +# Add a chart title and some axis labels. |
| 167 | +chart_title_set_name(chart, "Results of sample analysis") |
| 168 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_X), "Test number") |
| 169 | +chart_axis_set_name(chart_axis_get(chart, LXW_CHART_AXIS_TYPE_Y), "Sample length (mm)") |
| 170 | + |
| 171 | +# Set an Excel chart style. |
| 172 | +chart_set_style(chart, 15) |
| 173 | + |
| 174 | +# Insert the chart into the worksheet. |
| 175 | +worksheet_insert_chart(worksheet, CELL("E66")..., chart) |
| 176 | + |
| 177 | +workbook_close(workbook) |
0 commit comments