1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
use super::{convert_bool, convert_str, FormatColor, Workbook, WorksheetCol, WorksheetRow};

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub enum ChartType {
    None,
    Area,
    AreaStacked,
    AreaStackedPercent,
    Bar,
    BarStacked,
    Column,
    ColumnStacked,
    ColumnStackedPercent,
    Doughnut,
    Line,
    Pie,
    Scatter,
    ScatterStraight,
    ScatterStraightWithMarkers,
    ScatterSmooth,
    ScatterSmoothWithMarkers,
    Radar,
    RadarWithMarkers,
    RadarFilled,
}

impl ChartType {
    pub(crate) fn value(self) -> u8 {
        let value = match self {
            ChartType::None => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_NONE,
            ChartType::Area => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_AREA,
            ChartType::AreaStacked => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_AREA_STACKED,
            ChartType::AreaStackedPercent => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_AREA_STACKED_PERCENT
            }
            ChartType::Bar => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_BAR,
            ChartType::BarStacked => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_BAR_STACKED,
            ChartType::Column => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_COLUMN,
            ChartType::ColumnStacked => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_COLUMN_STACKED,
            ChartType::ColumnStackedPercent => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_COLUMN_STACKED_PERCENT
            }
            ChartType::Doughnut => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_DOUGHNUT,
            ChartType::Line => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_LINE,
            ChartType::Pie => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_PIE,
            ChartType::Scatter => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_SCATTER,
            ChartType::ScatterStraight => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_SCATTER_STRAIGHT
            }
            ChartType::ScatterStraightWithMarkers => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS
            }
            ChartType::ScatterSmooth => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_SCATTER_SMOOTH,
            ChartType::ScatterSmoothWithMarkers => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS
            }
            ChartType::Radar => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_RADAR,
            ChartType::RadarWithMarkers => {
                libxlsxwriter_sys::lxw_chart_type_LXW_CHART_RADAR_WITH_MARKERS
            }
            ChartType::RadarFilled => libxlsxwriter_sys::lxw_chart_type_LXW_CHART_RADAR_FILLED,
        };
        value as u8
    }
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ChartDashType {
    Solid,
    RoundDot,
    SquareDot,
    Dash,
    DashDot,
    LongDash,
    LongDashDot,
    LongDashDotDot,
}

impl ChartDashType {
    fn value(self) -> u8 {
        let value = match self {
            ChartDashType::Solid => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_SOLID
            }
            ChartDashType::RoundDot => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_ROUND_DOT
            }
            ChartDashType::SquareDot => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_SQUARE_DOT
            }
            ChartDashType::Dash => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_DASH
            }
            ChartDashType::DashDot => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_DASH_DOT
            }
            ChartDashType::LongDash => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_LONG_DASH
            }
            ChartDashType::LongDashDot => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_LONG_DASH_DOT
            }
            ChartDashType::LongDashDotDot => {
                libxlsxwriter_sys::lxw_chart_line_dash_type_LXW_CHART_LINE_DASH_LONG_DASH_DOT_DOT
            }
        };
        value as u8
    }
}

/// Struct to represent a chart line.

#[derive(Copy, Clone, PartialEq, PartialOrd)]
pub struct ChartLine {
    /// The chart font color.

    pub color: FormatColor,
    /// Turn off/hide line. Set to `false` or `true`.

    pub none: bool,
    /// Width of the line in increments of 0.25. Default is 2.25.

    pub width: f32,
    /// The line dash type.

    pub dash_type: ChartDashType,
    /// Set the transparency of the line. 0 - 100. Default 0.

    pub transparency: u8,
}

impl ChartLine {
    pub fn new() -> Self {
        ChartLine::default()
    }

    fn value(&self) -> libxlsxwriter_sys::lxw_chart_line {
        libxlsxwriter_sys::lxw_chart_line {
            color: self.color.value(),
            none: convert_bool(self.none),
            width: self.width,
            dash_type: self.dash_type.value(),
            transparency: self.transparency,
        }
    }
}

impl Default for ChartLine {
    fn default() -> Self {
        ChartLine {
            color: FormatColor::Black,
            none: false,
            width: 2.25,
            dash_type: ChartDashType::Solid,
            transparency: 0,
        }
    }
}

/// Struct to represent a chart fill.

#[derive(Clone, PartialEq, PartialOrd)]
pub struct ChartFill {
    /// The chart font color.

    pub color: FormatColor,
    /// Turn off/hide line. Set to false or true.

    pub none: bool,
    /// Set the transparency of the fill. 0 - 100. Default 0.

    pub transparency: u8,
}

impl ChartFill {
    pub fn new() -> Self {
        ChartFill::default()
    }

    fn value(&self) -> libxlsxwriter_sys::lxw_chart_fill {
        libxlsxwriter_sys::lxw_chart_fill {
            color: self.color.value(),
            none: convert_bool(self.none),
            transparency: self.transparency,
        }
    }
}

impl Default for ChartFill {
    fn default() -> Self {
        ChartFill {
            color: FormatColor::Black,
            none: false,
            transparency: 0,
        }
    }
}

/// Struct to represent an Excel chart data series.

/// This struct is created using the chart.add_series() function. It is used in functions that modify a chart series but the members of the struct aren't modified directly.

pub struct ChartSeries<'a> {
    pub(crate) _workbook: &'a Workbook,
    pub(crate) chart_series: *mut libxlsxwriter_sys::lxw_chart_series,
}

impl<'a> ChartSeries<'a> {
    /// The categories and values of a chart data series are generally set using the chart_add_series() function and Excel range formulas like "=Sheet1!$A$2:$A$7".

    ///

    /// The `ChartSeries.set_categories()` function is an alternative method that is easier to generate programmatically. It requires that you set the categories and values parameters in Chart.add_series() to `None` and then set them using row and column values in ChartSeries.set_categories() and ChartSeries.set_values():

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_categories-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series = chart.add_series(None, None);

    /// series.set_categories("Sheet1", 0, 0, 4, 0); // "=Sheet1!$A$1:$A$5"

    /// series.set_values("Sheet1", 0, 1, 4, 1);     // "=Sheet1!$B$1:$B$5"

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..5 {

    /// #     worksheet.write_string(i, 0, &format!("value {}", i + 1), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    pub fn set_categories(
        &mut self,
        sheet_name: &str,
        first_row: WorksheetRow,
        first_column: WorksheetCol,
        last_row: WorksheetRow,
        last_column: WorksheetCol,
    ) {
        let sheet_name_vec = convert_str(sheet_name);
        unsafe {
            libxlsxwriter_sys::chart_series_set_categories(
                self.chart_series,
                sheet_name_vec.as_ptr() as *const i8,
                first_row,
                first_column,
                last_row,
                last_column,
            );
        }
        self._workbook.const_str.borrow_mut().push(sheet_name_vec);
    }

    /// The categories and values of a chart data series are generally set using the `Chart.add_series()` function and Excel range formulas like "=Sheet1!$A$2:$A$7".

    ///

    /// The `Chart.series_set_values()` function is an alternative method that is easier to generate programmatically. See the documentation for `ChartSeries.set_categories()` above.

    pub fn set_values(
        &mut self,
        sheet_name: &str,
        first_row: WorksheetRow,
        first_column: WorksheetCol,
        last_row: WorksheetRow,
        last_column: WorksheetCol,
    ) {
        let sheet_name_vec = convert_str(sheet_name);
        unsafe {
            libxlsxwriter_sys::chart_series_set_values(
                self.chart_series,
                sheet_name_vec.as_ptr() as *const i8,
                first_row,
                first_column,
                last_row,
                last_column,
            );
        }
        self._workbook.const_str.borrow_mut().push(sheet_name_vec);
    }

    /// This function is used to set the name for a chart data series. The series name in Excel is displayed in the chart legend and in the formula bar. The name property is optional and if it isn't supplied it will default to `Series 1..n`.

    ///

    /// ```rust

    /// use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_name-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series = chart.add_series(None, Some("=Sheet1!$A$2:$A$6"));

    /// series.set_name("Quarterly budget data");

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 1..6 {

    /// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    ///

    /// The name parameter can also be a formula such as =Sheet1!$A$1 to point to a cell in the workbook that contains the name:

    /// ```rust

    /// use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_name-2.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series = chart.add_series(None, Some("=Sheet1!$A$2:$A$6"));

    /// series.set_name("=Sheet1!$A$1:$A$1");

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 1..6 {

    /// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    pub fn set_name(&mut self, name: &str) {
        let name_vec = convert_str(name);
        unsafe {
            libxlsxwriter_sys::chart_series_set_name(
                self.chart_series,
                name_vec.as_ptr() as *const i8,
            );
        }
        self._workbook.const_str.borrow_mut().push(name_vec);
    }

    /// The `ChartSeries.set_name_range()` function can be used to set a series name range and is an alternative to using `ChartSeries.set_name()` and a string formula:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_name_range-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series = chart.add_series(None, Some("=Sheet1!$B$2:$B$6"));

    /// series.set_name_range("Sheet1", 0, 1); // =Sheet1!$B$1

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 1..6 {

    /// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    pub fn set_name_range(&mut self, sheet_name: &str, row: WorksheetRow, column: WorksheetCol) {
        let sheet_name_vec = convert_str(sheet_name);
        unsafe {
            libxlsxwriter_sys::chart_series_set_name_range(
                self.chart_series,
                sheet_name_vec.as_ptr() as *const i8,
                row,
                column,
            );
        }
        self._workbook.const_str.borrow_mut().push(sheet_name_vec);
    }

    /// Set the line/border properties of a chart series:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_line-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series1 = chart.add_series(None, Some("=Sheet1!$A$2:$A$6"));

    /// let mut series2 = chart.add_series(None, Some("=Sheet1!$B$2:$B$6"));

    /// let mut series3 = chart.add_series(None, Some("=Sheet1!$C$2:$C$6"));

    /// let mut chart_line = ChartLine::new();

    /// chart_line.color = FormatColor::Red;

    /// series1.set_line(&chart_line);

    /// series2.set_line(&chart_line);

    /// series3.set_line(&chart_line);

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 1..6 {

    /// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// ![Result Image](https://github.com/informationsea/xlsxwriter-rs/raw/master/images/test-chart_series-set_line-1.png)

    pub fn set_line(&mut self, line: &ChartLine) {
        unsafe {
            libxlsxwriter_sys::chart_series_set_line(self.chart_series, &mut line.value());
        }
    }

    /// Set the fill properties of a chart series:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_fill-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// # let mut series1 = chart.add_series(None, Some("=Sheet1!$A$2:$A$6"));

    /// # let mut series2 = chart.add_series(None, Some("=Sheet1!$B$2:$B$6"));

    /// # let mut series3 = chart.add_series(None, Some("=Sheet1!$C$2:$C$6"));

    /// let mut chart_fill_1 = ChartFill::new();

    /// chart_fill_1.color = FormatColor::Red;

    /// let mut chart_fill_2 = ChartFill::new();

    /// chart_fill_2.color = FormatColor::Yellow;

    /// let mut chart_fill_3 = ChartFill::new();

    /// chart_fill_3.color = FormatColor::Green;

    /// series1.set_fill(&chart_fill_1);

    /// series2.set_fill(&chart_fill_2);

    /// series3.set_fill(&chart_fill_3);

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 1..6 {

    /// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// ![Result Image](https://github.com/informationsea/xlsxwriter-rs/raw/master/images/test-chart_series-set_fill-1.png)

    pub fn set_fill(&mut self, fill: &ChartFill) {
        unsafe {
            libxlsxwriter_sys::chart_series_set_fill(self.chart_series, &mut fill.value());
        }
    }

    /// Invert the fill color for negative values. Usually only applicable to column and bar charts.

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart_series-set_invert_if_negative-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// # let mut series1 = chart.add_series(None, Some("=Sheet1!$A$2:$A$6"));

    /// # let mut series2 = chart.add_series(None, Some("=Sheet1!$B$2:$B$6"));

    /// # let mut series3 = chart.add_series(None, Some("=Sheet1!$C$2:$C$6"));

    /// # let mut chart_fill_1 = ChartFill::new();

    /// # chart_fill_1.color = FormatColor::Red;

    /// # let mut chart_fill_2 = ChartFill::new();

    /// # chart_fill_2.color = FormatColor::Yellow;

    /// # let mut chart_fill_3 = ChartFill::new();

    /// # chart_fill_3.color = FormatColor::Green;

    /// # series1.set_fill(&chart_fill_1);

    /// series1.set_invert_if_negative();

    /// # series2.set_fill(&chart_fill_2);

    /// # series2.set_invert_if_negative();

    /// # series3.set_fill(&chart_fill_3);

    /// # series3.set_invert_if_negative();

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # worksheet.write_string(0, 0, "Set 1", None)?;

    /// # worksheet.write_string(0, 1, "Set 2", None)?;

    /// # worksheet.write_string(0, 2, "Set 3", None)?;

    /// # for i in 0..6 {

    /// #     let j: f64 = i.into();

    /// #     worksheet.write_number(i, 0, (j*10.) - 20., None)?;

    /// #     worksheet.write_number(i, 1, (j*10. + 2.) - 20., None)?;

    /// #     worksheet.write_number(i, 2, (j*10. + 4.) - 20., None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    pub fn set_invert_if_negative(&mut self) {
        unsafe {
            libxlsxwriter_sys::chart_series_set_invert_if_negative(self.chart_series);
        }
    }
}

/// The Chart object represents an Excel chart. It provides functions for adding data series to the chart and for configuring the chart.

///

/// A Chart object isn't created directly. Instead a chart is created by calling the Workbook.add_chart() function from a Workbook object. For example:

/// ```rust

/// use xlsxwriter::*;

/// # fn main() -> Result<(), XlsxError> {

/// let workbook = Workbook::new("test-chart.xlsx");

/// let mut worksheet = workbook.add_worksheet(None)?;

/// write_worksheet(&mut worksheet)?; // write worksheet contents

/// let mut chart = workbook.add_chart(ChartType::Column);

/// chart.add_series(None, Some("=Sheet1!$A$1:$A$5"));

/// chart.add_series(None, Some("=Sheet1!$B$1:$B$5"));

/// chart.add_series(None, Some("=Sheet1!$C$1:$C$5"));

/// worksheet.insert_chart(1, 3, &chart)?;

/// workbook.close()

/// # }

/// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

/// # for i in 0..5 {

/// #     worksheet.write_number(i, 0, (i*10).into(), None)?;

/// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

/// #     worksheet.write_number(i, 2, (i*10 + 4).into(), None)?;

/// # }

/// # Ok(())

/// # }

/// ```

/// The chart in the worksheet will look like this:

/// ![Result Image](https://github.com/informationsea/xlsxwriter-rs/raw/master/images/test-chart-1.png)

///

///

/// The basic procedure for adding a chart to a worksheet is:

///

/// Create the chart with Workbook.add_chart().

/// Add one or more data series to the chart which refers to data in the workbook using Chart.add_series().

/// Configure the chart with the other available functions shown below.

/// Insert the chart into a worksheet using Worksheet.insert_chart().

pub struct Chart<'a> {
    pub(crate) _workbook: &'a Workbook,
    pub(crate) chart: *mut libxlsxwriter_sys::lxw_chart,
}

impl<'a> Chart<'a> {
    /// In Excel a chart **series** is a collection of information that defines which data is plotted such as the categories and values. It is also used to define the formatting for the data.

    ///

    /// For an libxlsxwriter chart object the chart_add_series() function is used to set the categories and values of the series:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart-add_series-1.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// chart.add_series(Some("=Sheet1!$A$1:$A$5"), Some("=Sheet1!$B$1:$B$5"));

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..5 {

    /// #     worksheet.write_string(i, 0, &format!("value {}", i + 1), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// The series parameters are:

    ///

    /// *categories: This sets the chart category labels. The category is more or less the same as the X axis. In most Excel chart types the categories property is optional and the chart will just assume a sequential series from 1..n:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart-add_series-2.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// chart.add_series(None, Some("=Sheet1!$B$1:$B$5"));

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..5 {

    /// #     worksheet.write_string(i, 0, &format!("value {}", i + 1), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// * values: This is the most important property of a series and is the only mandatory option for every chart object. This parameter links the chart with the worksheet data that it displays.

    ///

    /// The categories and values should be a string formula like "=Sheet1!$A$2:$A$7" in the same way it is represented in Excel. This is convenient when recreating a chart from an example in Excel but it is trickier to generate programmatically. For these cases you can set the categories and values to None and use the ChartSeries.set_categories() and ChartSeries.set_values() functions:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart-add_series-3.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// let mut series = chart.add_series(None, None);

    /// series.set_categories("Sheet1", 0, 0, 4, 0); // "=Sheet1!$A$1:$A$5"

    /// series.set_values("Sheet1", 0, 1, 4, 1);     // "=Sheet1!$B$1:$B$5"

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..5 {

    /// #     worksheet.write_string(i, 0, &format!("value {}", i + 1), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// As shown in the previous example the return value from Chart.add_series() is a `ChartSeries` struct. This can be used in other functions that configure a series.

    ///

    /// More than one series can be added to a chart. The series numbering and order in the Excel chart will be the same as the order in which they are added in libxlsxwriter:

    ///

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart-add_series-4.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// chart.add_series(None, Some("=Sheet1!$A$1:$A$5"));

    /// chart.add_series(None, Some("=Sheet1!$B$1:$B$5"));

    /// chart.add_series(None, Some("=Sheet1!$C$1:$C$5"));

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..5 {

    /// #     worksheet.write_number(i, 0, (i*10 + 1).into(), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// #     worksheet.write_number(i, 2, (i*10 + 5).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```

    /// It is also possible to specify non-contiguous ranges:

    /// ```rust

    /// # use xlsxwriter::*;

    /// # fn main() -> Result<(), XlsxError> {

    /// # let workbook = Workbook::new("test-chart-add_series-5.xlsx");

    /// # let mut worksheet = workbook.add_worksheet(None)?;

    /// # write_worksheet(&mut worksheet)?; // write worksheet contents

    /// # let mut chart = workbook.add_chart(ChartType::Column);

    /// chart.add_series(Some("=(Sheet1!$A$1:$A$5,Sheet1!$A$10:$A$18)"), Some("=(Sheet1!$B$1:$B$5,Sheet1!$B$10:$B$18)"));

    /// # worksheet.insert_chart(1, 3, &chart)?;

    /// # workbook.close()

    /// # }

    /// # fn write_worksheet(worksheet: &mut Worksheet) -> Result<(), XlsxError> {

    /// # for i in 0..20 {

    /// #     worksheet.write_string(i, 0, &format!("value {}", i + 1), None)?;

    /// #     worksheet.write_number(i, 1, (i*10 + 2).into(), None)?;

    /// # }

    /// # Ok(())

    /// # }

    /// ```


    pub fn add_series(
        &mut self,
        categories: Option<&str>,
        values: Option<&str>,
    ) -> ChartSeries<'a> {
        let categories_vec = categories.map(convert_str);
        let values_vec = values.map(convert_str);
        let mut const_str = self._workbook.const_str.borrow_mut();
        let series = unsafe {
            libxlsxwriter_sys::chart_add_series(
                self.chart,
                categories_vec
                    .as_ref()
                    .map(|x| x.as_ptr())
                    .unwrap_or(std::ptr::null()) as *const i8,
                values_vec
                    .as_ref()
                    .map(|x| x.as_ptr())
                    .unwrap_or(std::ptr::null()) as *const i8,
            )
        };
        if let Some(x) = categories_vec {
            const_str.push(x);
        }
        if let Some(x) = values_vec {
            const_str.push(x);
        }
        ChartSeries {
            _workbook: self._workbook,
            chart_series: series,
        }
    }
}