Пример #1
0
/*
 * Create a worksheet with examples charts.
 */
int main() {

    lxw_workbook     *workbook  = new_workbook("chartsheet.xlsx");
    lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_chartsheet   *chartsheet = workbook_add_chartsheet(workbook, NULL);
    lxw_chart_series *series;

    /* Add a bold format to use to highlight the header cells. */
    lxw_format *bold = workbook_add_format(workbook);
    format_set_bold(bold);

    /* Write some data for the chart. */
    write_worksheet_data(worksheet, bold);

    /* Create a bar chart. */
    lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);

    /* Add the first series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");

    /* Set the name for the series instead of the default "Series 1". */
    chart_series_set_name(series, "=Sheet1!$B$1");

    /* Add a second series but leave the categories and values undefined. They
     * can be defined later using the alternative syntax shown below.  */
    series = chart_add_series(chart, NULL, NULL);

    /* Configure the series using a syntax that is easier to define programmatically. */
    chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
    chart_series_set_values(series,     "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
    chart_series_set_name_range(series, "Sheet1", 0, 2);       /* "=Sheet1!$C$1"      */

    /* Add a chart title and some axis labels. */
    chart_title_set_name(chart,        "Results of sample analysis");
    chart_axis_set_name(chart->x_axis, "Test number");
    chart_axis_set_name(chart->y_axis, "Sample length (mm)");

    /* Set an Excel chart style. */
    chart_set_style(chart, 11);

    /* Add the chart to the chartsheet. */
    chartsheet_set_chart(chartsheet, chart);

    /* Display the chartsheet as the active sheet when the workbook is opened. */
    chartsheet_activate(chartsheet);

    return workbook_close(workbook);
}
Пример #2
0
/*
 * Create a worksheet with examples charts.
 *
 */
int main() {

    lxw_workbook     *workbook  = new_workbook("chart_pie.xlsx");
    lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_chart *chart ;
    lxw_chart_series *series;

    /* Add a bold format to use to highlight the header cells. */
    lxw_format *bold = workbook_add_format(workbook);
    format_set_bold(bold);

    /* Write some data for the chart. */
    write_worksheet_data(worksheet, bold);


    /*
     * Chart 1: Create a simple pie chart.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_PIE);

    /* Add the first series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");

    /* Set the name for the series instead of the default "Series 1". */
    chart_series_set_name(series, "Pie sales data");

    /* Add a chart title. */
    chart_title_set_name(chart, "Popular Pie Types");

    /* Set an Excel chart style. */
    chart_set_style(chart, 10);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("D2"), chart);


    /*
     * Chart 2: Create a pie chart with user defined segment colors.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_PIE);

    /* Add the first series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");

    /* Set the name for the series instead of the default "Series 1". */
    chart_series_set_name(series, "Pie sales data");

    /* Add a chart title. */
    chart_title_set_name(chart, "Pie Chart with user defined colors");

    /* Add for fills for use in the chart. */
    lxw_chart_fill fill1 = {.color = 0x5ABA10};
    lxw_chart_fill fill2 = {.color = 0xFE110E};
    lxw_chart_fill fill3 = {.color = 0xCA5C05};

    /* Add some points with the above fills. */
    lxw_chart_point point1 = {.fill = &fill1};
    lxw_chart_point point2 = {.fill = &fill2};
    lxw_chart_point point3 = {.fill = &fill3};

    /* Create an array of the point objects. */
    lxw_chart_point *points[] = {&point1,
                                 &point2,
                                 &point3,
                                 NULL};

    /* Add/override the points/segments of the chart. */
    chart_series_set_points(series, points);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("D18"), chart);


    /*
     * Chart 3: Create a pie chart with rotation of the segments.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_PIE);

    /* Add the first series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");

    /* Set the name for the series instead of the default "Series 1". */
    chart_series_set_name(series, "Pie sales data");

    /* Add a chart title. */
    chart_title_set_name(chart, "Pie Chart with segment rotation");


    /* Change the angle/rotation of the first segment. */
    chart_set_rotation(chart, 90);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("D34"), chart);


    return workbook_close(workbook);
}