eb_bars 0.7.3

Tiny plot library for creating barcharts and histograms
Documentation
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
// Copyright 2025 Developers of eb_bars.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! EB - Bars: Plotting library for Rust providing a simple way to create barcharts in svg format.
//!
//! # Quick Start
//!
//! The simplest usecase (which you never really might want) is written like so.
//! ```
//! use eb_bars::BarPlot;
//!
//! // Start out with an empty plot.
//! let mut plot = BarPlot::new();
//!
//! // Add a set of values.
//! plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);
//!
//! // Render to svg format.
//! let svg: String = plot.to_svg(1600, 1000);
//! ```
//!
//! # But the above "Quick Start" looks bad and boring
//!
//! As the above example stands, the largest number will have its bar take up the full height of the window.
//! Also, the lowest value will be a bar of zero height e.g. no bar at all.
//! The problem is that we have not set a specific scaling for the values.
//! This means that the bars are all scaled based on the minimum and maximum value.
//! Let's improve it a bit in the next section by adding a scale.
//!
//! ```
//! use eb_bars::BarPlot;
//!
//! let mut plot = BarPlot::new();
//!
//! // Add same values as before.
//! plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);
//!
//! // Here, we are setting a scale range for better visibility/scaling of bars.
//! plot.set_scale_range(0, 20, 2);
//! // The bars will look better, but the numbers on the scale won't be visible.
//! // We need to shrink the plot window relative to the full window.
//! // Keep in mind that all size and offset values are based of a percentage.
//! // Setting a width to 100 means it takes up the whole width.
//! // Same goes for the height.
//!
//! // Let's shrink the plot size.
//! plot.set_plot_window_size(95.0, 85.0, 93.0, 50.0);
//! // We have now set the width at 95% and moved it 85% right from the left side.
//! // We also set the height at 93% and moved it 50% down from the top.
//! // First two parameters affect the width and the left/right offset respectively.
//! // The last two parameters affect the height and the top/bottom offset respectively.
//!
//! // Let's render the svg.
//! let svg: String = plot.to_svg(1600, 1000);
//! ```
//!
//! It is still kinda boring, so please checkout all the tweaks available in [`BarPlot`].
//!
//! # Important note
//!
//! If the method name is prefixed `add_`, then calling it multiple times will _add_ stuff to the plot.
//! This goes for adding multiple sets of values (categories) and adding colors to those values etc..
//!
//! If the method name is prefixed `set_`, then calling it multiple times will _override_ the previous one.
//! You most certainly never want to call these more than once, unless there is a good reason to.
//!
//! Check out [`BarPlot`] for all implementations.
//!
//! # Panics and error handling.
//!
//! This library has very limited error handling at the moment. Actually, it has none.
//! There are some assertions here and there that will provoke a panic on invalid input.
//! That way, you can try-and-re-try your code until it works.
//!
//! Once everything works, it is very unlikely that it will panic on continous use in your application.
//! However, if you pass values that are generated from a source that you do not have full control over,
//! then the task of making sure the input is sanitized and double checked lies on your end and your code.

mod svg;

type Percentage = f64;

const VERSION: &str = "0.7.3";
const REPOSITORY: &str = "https://github.com/emilbratt/eb_bars";

const DEFAULT_SIZE: (u32, u32) = (1600, 1000);

const DEFAULT_BAR_COLOR: &str = "rgb(112, 153, 182)";
const DEFAULT_BASE_COLOR: &str = "rgb(197, 197, 197)";
const DEFAULT_BAR_GAP: Percentage = 0.0;
const DEFAULT_BIN_GAP: Percentage = 10.0;

const DEFAULT_FONT_SIZE: Percentage = 100.0;
const DEFAULT_LEGEND_POSITION: (Percentage, Percentage) = (90.0, 20.0);
const DEFAULT_TEXT_SIDE_OFFSET: Percentage = 35.0;
const DEFAULT_TICK_LENGTH: Percentage = 10.0;


#[derive(Debug, Default)]
enum BinMarkerPosition {
    Left,
    #[default]
    Middle,
    Right,
}

#[derive(Debug, Default)]
struct PlotLegend<'a> {
    categories: Option<&'a[&'a str]>,
    position: Option<(Percentage, Percentage)>,
}

#[derive(Debug)]
enum BarColorLayout<'a> {
    Category(Vec<&'a str>), // Each category has its own color.
    Indexed(Vec<Vec<&'a str>>), // Every bar has its own selected color.
    Threshold((&'a str, &'a str, &'a str, &'a str)), // Every bar is given its color based on its value.
    Uniform(&'a str), // All bars are the same color.
}

impl Default for BarColorLayout<'_> {
    fn default() -> Self {
        Self::Uniform(DEFAULT_BAR_COLOR)
    }
}

#[derive(Debug, Default)]
struct BarColors<'a> {
    layout: BarColorLayout<'a>,
    overrides: Vec<(usize, usize, &'a str)>,
}

#[derive(Debug)]
struct Colors<'a> {
    background: Option<&'a str>,
    bars: BarColors<'a>,
    line: &'a str,
    text: &'a str,
    tick: &'a str,
}

impl Default for Colors<'_> {
    fn default() -> Self {
        Self {
            background: None,
            bars: BarColors::default(),
            line: DEFAULT_BASE_COLOR,
            text: DEFAULT_BASE_COLOR,
            tick: DEFAULT_BASE_COLOR,
        }
    }
}

#[derive(Debug, Default)]
struct Show {
    window_border: bool,
    plot_border: bool,
    horizontal_lines: bool,
    vertical_lines: bool,
}

#[derive(Debug, Default)]
struct PlotText<'a> {
    left: Option<&'a str>,
    left_offset: Option<Percentage>,

    right: Option<&'a str>,
    right_offset: Option<Percentage>,

    top: Option<&'a str>,
    top_offset: Option<Percentage>,

    bottom: Option<&'a str>,
    bottom_offset: Option<Percentage>,
}

#[derive(Debug)]
struct PlotLayout {
    bar_gap: Percentage,
    bin_gap: Percentage,
    bin_marker_position: BinMarkerPosition,
    font_size: Percentage,
    plot_window_scale: Option<(Percentage, Percentage, Percentage, Percentage)>,
    scale_range: Option<(i64, i64, usize)>,
    x_axis_tick_length: Percentage,
    y_axis_tick_length: Percentage,
    negative_bars_go_down: bool,
}

impl Default for PlotLayout {
    fn default() -> Self {
        Self {
            bin_gap: DEFAULT_BIN_GAP,
            bar_gap: DEFAULT_BAR_GAP,
            bin_marker_position: BinMarkerPosition::default(),
            font_size: DEFAULT_FONT_SIZE,
            plot_window_scale: None,
            scale_range: None,
            x_axis_tick_length: DEFAULT_TICK_LENGTH,
            y_axis_tick_length: DEFAULT_TICK_LENGTH,
            negative_bars_go_down: false,
        }
    }
}

#[derive(Debug)]
enum LinesAt<'a> {
    Horizontal(f64, &'a str),
    Vertical(f64, &'a str),
}

#[derive(Debug)]
pub struct BarPlot<'a> {
    values: Vec<&'a [f64]>,
    markers: Option<&'a [&'a str]>,
    lines_at: Vec<LinesAt<'a>>,
    size: (u32, u32),
    colors: Colors<'a>,
    legend: PlotLegend<'a>,
    layout: PlotLayout,
    show: Show,
    plot_text: PlotText<'a>,
}

// FIXME: add new with default, allow for now with attribute below..
#[allow(clippy::new_without_default)]
impl <'a>BarPlot<'a> {

    /// Instantiate a new plot.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    /// ```
    pub fn new() -> Self {
        Self {
            values: Vec::new(),
            markers: None,
            lines_at: Vec::new(),
            size: DEFAULT_SIZE,
            colors: Colors::default(),
            legend: PlotLegend::default(),
            layout: PlotLayout::default(),
            show: Show::default(),
            plot_text: PlotText::default(),
        }
    }

    /// Adding a set of values (bars) to the plot.
    ///
    /// # Takes an array slice of f64.
    ///
    /// All valus must be f64.
    /// If you have a `Vec<u32>`, make sure to convert it to a `Vec<f64>` before passing it.
    /// Then you can pass the vector as a reference.
    ///
    /// # This method is required.
    ///
    /// There must be at least one set of values to produce a plot. :)
    ///
    /// # Grouped bars
    ///
    /// Calling this method more than once will create `groups` for values of same index.
    /// This means that the first datapoint of each added dataset will be the first group,
    /// the second datapoint of each added dataset will be the second group and so on..
    /// E.g. calling this method 5 times will add groups of 5 bars in each bin.
    ///
    /// # Short summary
    ///
    /// * Must be called at least once. A plot without values does not make any sense.. :)
    /// * If called multiple times, each bin will contain a group with values of the same index.
    /// * All arrays passed after first call must be of the `exact` same length as the first array.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    /// // The first group contains 5 apples and 7 oranges.
    /// // The second one 16 and 6 respectively.
    /// // The last group contains 3 apples and 9 oranges.
    ///
    /// // Add the first set of values.
    /// plot.add_values(&apples);
    ///
    /// // Add the second set of values.
    /// plot.add_values(&oranges);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_values(&mut self, values: &'a [f64]) {
        if !self.values.is_empty() {
            // This makes sure that all new values are of same length e.g. same count as previous values.
            // This is needed so that all bins have the same amount of bars in them,
            // ..as long as we add more than 1 category, that is..
            let exp_count = self.values[0].len();
            let count = values.len();
            assert_eq!(
                exp_count,
                count,
                "Added values should be same count as old, expected {exp_count}, got {count}"
            );
        }
        self.values.push(values);
    }

    /// Set a fill color as background.
    ///
    /// By default, the image will be fully transparent where there is nothing drawn on it.
    /// Adding a background color might be a good idea for better visual presentation depending on usecase.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// plot.set_background_color("Black");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_background_color(&mut self, color: &'a str) {
        self.colors.background = Some(color);
    }

    /// Set color for the lines.
    ///
    /// By default, all lines are drawn with a `default` color.
    /// You can `override` this by setting your own color.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// plot.set_line_color("Yellow");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_line_color(&mut self, color: &'a str) {
        self.colors.line = color;
    }

    /// Set color for text/numbers.
    ///
    /// By default, all text are drawn with a `default` color.
    /// You can `override` this by setting your own color.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// plot.set_text_color("LightBlue");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_color(&mut self, color: &'a str) {
        self.colors.text = color;
    }

    /// Set color for x-ticks and y-ticks.
    ///
    /// By default, all ticks are drawn with a `default` color.
    /// You can `override` this by setting your own color.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// plot.set_tick_color("LightBlue");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_tick_color(&mut self, color: &'a str) {
        self.colors.tick = color;
    }

    /// Set a single color for all bars.
    ///
    /// By default, all bars are drawn with a `default` color.
    /// You can `override` this by setting a new uniform color value.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// plot.set_bar_colors_by_uniform("Green");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bar_colors_by_uniform(&mut self, color: &'a str) {
        self.colors.bars.layout = BarColorLayout::Uniform(color);
    }

    /// Set bar colors by threshold.
    ///
    /// By default, all bars are drawn with a `default` color.
    /// You can `override` this by setting different colors for different thresholds.
    /// The threshold is as follows: minumum value, less than average, greater than or equal to average and max.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Avoid if having more than one set of values.
    ///
    /// When you add multiple sets of values e.g. calling [`BarPlot::add_values`] multiple times,
    /// then you might want to use [`BarPlot::add_bar_colors_by_category`] instead.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// // Each color represent how signifacant a value is.
    /// let min = "Red"; // The lowest value will have its bar colored red.
    /// let low = "Orange"; // Low values will have their bars be orange.
    /// let high = "Yellow"; // High values will be yellow.
    /// let max = "Green"; // Max value (tallest bar) will be green.
    ///
    /// plot.set_bar_colors_by_threshold(min, low, high, max);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bar_colors_by_threshold(&mut self, min: &'a str, low: &'a str, high: &'a str, max: &'a str) {
        self.colors.bars.layout = BarColorLayout::Threshold((min, low, high, max));
    }

    /// Add color to last added values.
    ///
    /// By default, all bars are drawn with a `default` color.
    /// You can `override` this by setting different colors for different categories.
    /// Keep in mind that this only make sens to use if you have called [`BarPlot::add_values`]
    /// at least 2 times. The two datasets are treated as two distinct categories.
    /// Note: see [`BarPlot::set_legend`] for displaying the names with their respective color.
    ///
    /// # How it operates
    /// The category added first will have its bars colored first.
    /// Then, any consecutive call will apply proportional to all consecutive calls to [`BarPlot::add_values`].
    ///
    /// In simple terms: if you call [`BarPlot::add_values`] 5 times, then it is required to call this method
    /// `exactly` 5 times. Each time with its own particular color.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Avoid if having only one set of values.
    ///
    /// If you only add one set of values e.g. calling [`BarPlot::add_values`] one time,
    /// then it makes no sense using this method. Use any of the other ways to set colors.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    ///
    /// // Add first set of values.
    /// plot.add_values(&apples);
    /// // Adding a second set of values.
    /// plot.add_values(&oranges);
    ///
    /// // First call adds a color to the first category. Red bacuse apples are often red.
    /// plot.add_bar_colors_by_category("Red");
    /// // Second call adds a color to the second category. Orange because; oranges are orange. :)
    /// plot.add_bar_colors_by_category("Orange");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_bar_colors_by_category(&mut self, color: &'a str) {
        if let BarColorLayout::Category(v) = &mut self.colors.bars.layout {
            v.push(color);
        } else {
            self.colors.bars.layout = BarColorLayout::Category(vec![color]);
        }
    }

    /// Add a set of colors for every bar in last added category.
    ///
    /// By default, all bars are drawn with a `default` color.
    /// You can `override` this by adding an array of colors which have same length as the values.
    ///
    /// # How it operates
    /// The category added first will have its bars colored first.
    /// Then, any consecutive call will apply proportional to all consecutive calls to [`BarPlot::add_values`].
    ///
    /// In simple terms: if you call [`BarPlot::add_values`] 5 times, then it is required to call this method
    /// `exactly` 5 times. Each time with its own particular set of colors.
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// This method is meant for users that want full control over the bar colors.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    ///
    /// // Add first set of values.
    /// plot.add_values(&apples);
    /// // Adding a second set of values.
    /// plot.add_values(&oranges);
    ///
    /// // First call adds a color to the first category. Red bacuse apples are often red.
    /// let clr_red = vec!["Red", "Red", "Red", "Red", "Red"];
    /// plot.add_bar_colors_from_vec(clr_red);
    /// // Second call adds a color to the second category. Orange because; oranges are orange. :)
    /// let clr_orange = vec!["Orange", "Orange", "Orange", "Orange", "Orange"];
    /// plot.add_bar_colors_from_vec(clr_orange);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_bar_colors_from_vec(&mut self, colors: Vec<&'a str>) {
        if let BarColorLayout::Indexed(v) = &mut self.colors.bars.layout {
            v.push(colors);
        } else {
            self.colors.bars.layout = BarColorLayout::Indexed(vec![colors]);
        }
    }

    /// Override any bar with any color.
    ///
    /// This one will override the category index and the value (bar) index with a color.
    /// Say you added 3 sets of values by calling [`BarPlot::add_values`] 3 times.
    /// This means you have 3 categories times `X` values where `X` is the length of array holding
    /// each of set of values.
    ///
    /// # How it operates
    /// The category added first will have its category and bars colored zero indexed.
    /// The first bar in the first category will have index 0-0. The next bar will have 0-1.
    /// You call this method as many times as you need, passing one color at the time.
    ///
    /// Calling [`BarPlot::add_values`] 5 times with 3 values for each set of values,
    /// then the index for category will be 0-4 (5 indexes ) and the index for values will be 0-2 (3 indexes).
    ///
    /// # Accepted color conventions.
    ///
    /// * As its name such as "Red".
    /// * As an RGB value such as "rgb(29, 28, 27)".
    /// * As a HEX value such as "#1111FA".
    /// * As an HSL value such as "hsl(30, 3.80%, 10.20%)".
    ///
    /// # Avoid if having only one set of values.
    ///
    /// If you only add one set of values e.g. calling [`BarPlot::add_values`] one time,
    /// then it makes no sense using this method. Use any of the other ways to set colors.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    ///
    /// // Add first set of values.
    /// plot.add_values(&apples);
    /// // Adding a second set of values.
    /// plot.add_values(&oranges);
    ///
    /// // First call adds a color to the first category. Red bacuse apples are often red.
    /// let clr_red = vec!["Red", "Red", "Red", "Red", "Red"];
    /// plot.add_bar_colors_from_vec(clr_red);
    /// // Second call adds a color to the second category. Orange because; oranges are orange. :)
    /// let clr_orange = vec!["Orange", "Orange", "Orange", "Orange", "Orange"];
    /// plot.add_bar_colors_from_vec(clr_orange);
    ///
    /// // Setting the first apple = green.
    /// plot.add_bar_color_override(0, 0, "Green");
    /// // Setting the last orange to blue.. :)
    /// plot.add_bar_color_override(1, 4, "Blue");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_bar_color_override(&mut self, category: usize, bar: usize, color: &'a str) {
        assert!(
            category < self.values.len(),
            "{}\n{}",
            format!("Can not add category index '{category}' as there are not enough categories added yet."),
            "This may result in current override being ignored.",
        );

        self.colors.bars.overrides.push((category, bar, color));
    }

    /// Show horizontal grid lines.
    ///
    /// # Important
    ///
    /// Call [`BarPlot::set_scale_range`] first, otherwise there are no values to base the grid on.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);
    ///
    /// // Needed for horizontal (y-grid) lines.
    /// plot.set_scale_range(0, 20, 2);
    ///
    /// plot.set_show_horizontal_lines();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_show_horizontal_lines(&mut self) {
        self.show.horizontal_lines = true;
    }

    /// Add custom horizontal grid lines.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6];
    /// plot.add_values(&values);
    ///
    /// // Add a horizontal lines.
    /// let line_color = "rgb(126, 255, 165)";
    /// plot.add_horizontal_line_at(25.0, line_color);
    /// plot.add_horizontal_line_at(50.0, line_color);
    /// plot.add_horizontal_line_at(75.0, line_color);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_horizontal_line_at(&mut self, p: Percentage, color: &'a str) {
        self.lines_at.push(LinesAt::Horizontal(p, color));
    }

    /// Show vertical grid lines.
    ///
    /// # Important
    ///
    ///  Call [`BarPlot::set_bin_markers`] first, otherwise there are no values to base the grid on.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6];
    /// plot.add_values(&values);
    ///
    /// // Needed for vertical (x-grid) lines.
    /// let markers: Vec<String> = (0..values.len()).map(|i| (i).to_string()).collect();
    /// let markers = markers.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
    /// plot.set_bin_markers(&markers);
    ///
    /// plot.set_show_vertical_lines();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_show_vertical_lines(&mut self) {
        self.show.vertical_lines = true;
    }

    /// Add custom vertical grid lines.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6];
    /// plot.add_values(&values);
    ///
    /// // Add vertical lines.
    /// let line_color = "rgb(126, 255, 165)";
    /// plot.add_vertical_line_at(25.0, line_color);
    /// plot.add_vertical_line_at(50.0, line_color);
    /// plot.add_vertical_line_at(75.0, line_color);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn add_vertical_line_at(&mut self, p: Percentage, color: &'a str) {

        self.lines_at.push(LinesAt::Vertical(p, color));
    }

    /// Set size of the barplot size (relative to the canvas/frame).
    ///
    /// By default, the barchart part of the image will take up the full width and length of the frame.
    /// However, by adding literally anything to the barplot, you most likely want to change the size of the plot.
    /// If you skip this method, the text, ticks, markers, legend etc. will not be inside the viewbox of the canvas.
    ///
    /// # How it operates
    ///
    /// We only work with percentage values when defining sizes. A width of 100 means it takes up 100% of the width.
    /// A height of 100 will do the same, but for height.
    /// Same goes for offset. A horizontal offset of 10 means that the offset is pushed 10% from the left.
    /// A vertical offset of 10 means that the offset is pushed 10% from the top.
    /// An offset with value 100 means the offset is pushed all the way to the right (horizontal) or bottom (vertical).
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// let width = 90.0; // Set width to 90%,
    /// let horizontal_offset = 65.0; // Move 65% right
    /// let height = 85.0; // Set height to 85%
    /// let vertical_offset = 40.0; // Move 40% down.
    /// plot.set_plot_window_size(width, horizontal_offset, height, vertical_offset);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_plot_window_size(
        &mut self,
        x_length: Percentage,
        x_offset: Percentage,
        y_length: Percentage,
        y_offset: Percentage
    ) {
        assert!(x_length <= 100.0 && x_offset <= 100.0, "plot window width cannot exceed 100%");
        assert!(y_length <= 100.0 && y_offset <= 100.0, "plot window height cannot exceed 100%");

        self.layout.plot_window_scale = Some((x_length, x_offset, y_length, y_offset));
    }

    /// Set a scale for the barchart.
    ///
    /// By default, the scale is calculated using the maximum and minimum value throughout all data.
    /// However, by setting the scale manually we get a more presentable plot.
    /// Pass whole numbers (can be negative) as the scale minimum and scale maximum.
    /// The 3rd parameter is the gap between each number on the scale e.g. the step.
    /// The step must be a positive number as it is an increasing number starting from minimum.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[5.0, 16.4, 17.1, 13.7, 8.9, 3.9, 6.3, 9.6]);
    ///
    /// let min = 0;
    /// let max = 20;
    /// let step = 2;
    ///
    /// plot.set_scale_range(min, max, step);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_scale_range(&mut self, min: i64, max: i64, step: u64) {
        self.layout.scale_range = Some((min, max, step as usize));
    }

    /// Set the labels and markers for each bin/bucket on the x-axis.
    ///
    /// Note: Passing an array with fewer bin markers than added values will cause some bins to be un-labeled.
    /// To make sure everything is correct, it is recommended to pass the same amount of markers as values.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let absence_boys = [5., 3., 8., 4., 7.];
    /// let absence_girls = [4., 1., 2., 3., 1.];
    /// let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",];
    ///
    /// plot.add_values(&absence_boys);
    /// plot.add_values(&absence_girls);
    /// plot.set_bin_markers(&weekdays);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bin_markers(&mut self, markers: &'a [&'a str]) {
        self.markers = Some(markers);
    }

    /// Place the bin markers at the middle of each bin/bucket instead of to the left.
    ///
    /// By default, the bin markers are placed on the left side in between each bin.
    /// You can have the markers placed either `left`, `middle` or `right` depending on usecase.
    ///
    /// Note: check out [`BarPlot::set_bin_markers_left`] and [`BarPlot::set_bin_markers_right`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [1., 2., 3.];
    /// plot.add_values(&values);
    ///
    /// let markers: Vec<String> = (0..values.len()).map(|i| (i).to_string()).collect();
    /// let markers = markers.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
    /// plot.set_bin_markers(&markers);
    ///
    /// plot.set_bin_markers_middle();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bin_markers_middle(&mut self) {
        self.layout.bin_marker_position = BinMarkerPosition::Middle;
    }

    /// Place the bin markers to the left of each bin/bucket.
    ///
    /// By default, the bin markers are already placed on the left side in between each bin.
    /// This method can be used to _reset_ an eventual change.
    /// You can have the markers placed either `left`, `middle` or `right` depending on usecase.
    ///
    /// Note: check out [`BarPlot::set_bin_markers_middle`] and [`BarPlot::set_bin_markers_right`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [1., 2., 3.];
    /// plot.add_values(&values);
    ///
    /// let markers: Vec<String> = (0..values.len()).map(|i| (i).to_string()).collect();
    /// let markers = markers.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
    /// plot.set_bin_markers(&markers);
    ///
    /// // Setting markers at middle.
    /// plot.set_bin_markers_middle();
    /// // Then back to left side.
    /// plot.set_bin_markers_left();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bin_markers_left(&mut self) {
        self.layout.bin_marker_position = BinMarkerPosition::Left;
    }

    /// Place the bin markers to the right of each bin/bucket instead of to the left.
    ///
    /// By default, the bin markers are placed on the left side in between each bin.
    /// You can have the markers placed either `left`, `middle` or `right` depending on usecase.
    ///
    /// Note: check out [`BarPlot::set_bin_markers_left`] and [`BarPlot::set_bin_markers_middle`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let values = [1., 2., 3.];
    /// plot.add_values(&values);
    ///
    /// let markers: Vec<String> = (0..values.len()).map(|i| (i).to_string()).collect();
    /// let markers = markers.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
    /// plot.set_bin_markers(&markers);
    ///
    /// plot.set_bin_markers_right();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bin_markers_right(&mut self) {
        self.layout.bin_marker_position = BinMarkerPosition::Right;
    }

    /// Introduce a `gap` between every bar.
    ///
    /// The gap is calculated using a percentage.
    /// A gap of 0 means there is no gap/air between bars.
    /// A gap of 50 means that the bar and the gap will take up the same width.
    /// A gap of 100 means that the gap will take up all space and so the bar becomes invisible.
    ///
    /// Note: check out [`BarPlot::set_bin_gap`] for gap only betweem bins.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    /// plot.add_values(&[4., 5., 6.]);
    ///
    /// let gap = 30.0; // The gap will take up 30% of the space, leaving 70% for bar.
    /// plot.set_bar_gap(gap);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bar_gap(&mut self, gap: Percentage) {
        self.layout.bar_gap = gap;
    }

    /// Introduce a `gap` between every bin/bucket.
    ///
    /// The gap is calculated using a percentage.
    /// A gap of 0 means there is no gap/air between bins.
    /// A gap of 50 means that the bin and the gap will take up the same width.
    /// A gap of 100 means that the gap will take up all space and so the bin squashed into nothing.
    ///
    /// Note: check out [`BarPlot::set_bar_gap`] for gap betweem bars.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    /// plot.add_values(&[4., 5., 6.]);
    ///
    /// let gap = 30.0; // The gap will take up 30% of the space, leaving 70% for bin.
    /// plot.set_bin_gap(gap);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_bin_gap(&mut self, gap: Percentage) {
        self.layout.bin_gap = gap;
    }

    /// Set length for ticks on the y axis.
    ///
    /// The length is calculated using a percentage.
    /// A length of 0 means the tick will not be generated.
    /// A length of 10 means that the tick will be of a _somewhat_ _normal_ length.
    /// A length of greater than 10 means that the tick will be long.
    /// Try different lengths to find the best length for your usecase.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// let len = 20.0; // The tick length will be of considerate length.
    /// plot.set_y_axis_tick_length(len);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_y_axis_tick_length(&mut self, p: Percentage) {
        self.layout.y_axis_tick_length = p;
    }

    /// Set length for ticks on the x axis.
    ///
    /// The length is calculated using a percentage.
    /// A length of 0 means the tick will not be generated.
    /// A length of 10 means that the tick will be of a _somewhat_ _normal_ length.
    /// A length of greater than 10 means that the tick will be long.
    /// Try different lengths to find the best length for your usecase.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// let len = 20.0; // The tick length will be of considerate length.
    /// plot.set_x_axis_tick_length(len);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_x_axis_tick_length(&mut self, p: Percentage) {
        self.layout.x_axis_tick_length = p;
    }

    /// Anchor bars at zero instead of the floor.
    /// This will make negative bars grow downwards.
    /// If your dataset contains only negative values, then this might not make sense to use.
    /// Rather, `use this when your dataset is likely to contain both positive and negative values`.
    ///
    /// An area where it makes sens is when visualizing temperature differences. :)
    ///
    /// By default, bars are anchored at the floor of the barchart.
    /// However, you might want negative values to stand out by having them point downwards.
    /// This method will apply anchoring bars at the zero line instead of the floor.
    ///
    /// # Important
    /// Call [`BarPlot::set_scale_range`] first, and make sure to set `min < 0` and `max >= 0`.
    /// Otherwise, you ~might~ will get a barchart that looks goofy. Consider yourself warned.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[5.0, -16.4, 17.1, 13.7, 8.9, 3.9, -6.3, 9.6]);
    ///
    /// // Adding some extra tweaks that makes this example more clear.
    /// let min = -20;
    /// let max = 20;
    /// let step = 2;
    /// plot.set_scale_range(min, max, step);
    /// plot.set_plot_window_size(90.0, 80.0, 83.0, 50.0);
    ///
    /// // Negative bars will now grow downwards instead of upwards. :)
    /// plot.set_negative_bars_go_down();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_negative_bars_go_down(&mut self) {
        self.layout.negative_bars_go_down = true;
    }

    /// Apply text on the left side of the plot window.
    ///
    /// By default, the plot window takes up the whole window.
    /// For the text to be visible, we need to scale down the plot first with [`BarPlot::set_plot_window_size`].
    ///
    /// The text will by default be offset from the plot window with a sane value.
    /// If you want to override this value, you can set a custom offset with [`BarPlot::set_text_left_offset`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_left("This is some text.");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_left(&mut self, text: &'a str) {
        self.plot_text.left = Some(text);
    }

    /// Apply offset for text on the left side of the plot window.
    ///
    /// The offset is calculated percent wise from the frame towards the plot window.
    /// The higher the percentage the closer the text moves towards the plot window.
    ///
    /// Note: you need to explicitly apply text first with [`BarPlot::set_text_lef.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_left("This is some text.");
    ///
    /// let percent = 30.0;
    /// plot.set_text_left_offset(percent);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_left_offset(&mut self, offset: Percentage) {
        self.plot_text.left_offset = Some(offset);
    }

    /// Apply text on the right side of the plot window.
    ///
    /// By default, the plot window takes up the whole window.
    /// For the text to be visible, we need to scale down the plot first with [`BarPlot::set_plot_window_size`].
    ///
    /// The text will by default be offset from the plot window with a sane value.
    /// If you want to override this value, you can set a custom offset with [`BarPlot::set_text_right_offset`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_right("This is some text.");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_right(&mut self, text: &'a str) {
        self.plot_text.right = Some(text);
    }

    /// Apply offset for text on the right side of the plot window.
    ///
    /// The offset is calculated percent wise from the frame towards the plot window.
    /// The higher the percentage the closer the text moves towards the plot window.
    ///
    /// Note: you need to explicitly apply text first with [`BarPlot::set_text_right`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_right("This is some text.");
    ///
    /// let percent = 30.0;
    /// plot.set_text_right_offset(percent);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_right_offset(&mut self, offset: Percentage) {
        self.plot_text.right_offset = Some(offset);
    }

    /// Apply text underneath the plot window.
    ///
    /// By default, the plot window takes up the whole window.
    /// For the text to be visible, we need to scale down the plot first with [`BarPlot::set_plot_window_size`].
    ///
    /// The text will by default be offset from the plot window with a sane value.
    /// If you want to override this value, you can set a custom offset with [`BarPlot::set_text_bottom_offset`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_bottom("This is some text.");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_bottom(&mut self, text: &'a str) {
        self.plot_text.bottom = Some(text);
    }

    /// Apply offset for text underneath the plot window.
    ///
    /// The offset is calculated percent wise from the frame towards the plot window.
    /// The higher the percentage the closer the text moves towards the plot window.
    ///
    /// Note: you need to explicitly apply text first with [`BarPlot::set_text_bottom`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_bottom("This is some text.");
    ///
    /// let percent = 30.0;
    /// plot.set_text_bottom_offset(percent);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_bottom_offset(&mut self, offset: Percentage) {
        self.plot_text.bottom_offset = Some(offset);
    }

    /// Apply text above the plot window.
    ///
    /// By default, the plot window takes up the whole window.
    /// For the text to be visible, we need to scale down the plot first with [`BarPlot::set_plot_window_size`].
    ///
    /// The text will by default be offset from the plot window with a sane value.
    /// If you want to override this value, you can set a custom offset with [`BarPlot::set_text_top_offset`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_top("This is some text.");
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_top(&mut self, text: &'a str) {
        self.plot_text.top = Some(text);
    }

    /// Apply offset for text above the plot window.
    ///
    /// The offset is calculated percent wise from the frame towards the plot window.
    /// The higher the percentage the closer the text moves towards the plot window.
    ///
    /// Note: you need to explicitly apply text first with [`BarPlot::set_text_top`].
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// plot.set_text_top("This is some text.");
    ///
    /// let percent = 30.0;
    /// plot.set_text_top_offset(percent);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_text_top_offset(&mut self, offset: Percentage) {
        self.plot_text.top_offset = Some(offset);
    }

    /// Apply a legend with category names and their corresponding colors.
    ///
    /// When calling [`BarPlot::add_values`] multiple times each time adding a set of values (categories),
    /// you most likely want a legend to label each category by name and color.
    ///
    /// Note: The legend will use the colors added with [`BarPlot::add_bar_colors_by_category`],
    /// remember to call this method once for each category added.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    ///
    /// // Add first set of values.
    /// plot.add_values(&apples);
    /// // Adding a second set of values.
    /// plot.add_values(&oranges);
    ///
    /// // First call adds a color to the first category. Red bacuse apples are often red.
    /// plot.add_bar_colors_by_category("Red");
    /// // Second call adds a color to the second category. Orange because; oranges are orange. :)
    /// plot.add_bar_colors_by_category("Orange");
    ///
    /// // Adding the labels (colors are already added) for legend.
    /// let categories = ["Apples", "Oranges"];
    /// // Applying the legend.
    /// plot.set_legend(&categories);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_legend(&mut self, categories: &'a [&'a str]) {
        self.legend.categories = Some(categories);
    }

    /// Set legend position.
    ///
    /// When calling [`BarPlot::set_legend`], its position is calculated using percentage.
    /// By default, the legend is positioned on the far right side and close to the top.
    /// You can override the default position by passing two values.
    /// First value is the absolute offset from left to right,
    /// the second value is the absolute offset from top to bottom.
    ///
    /// An offset X = 50 and offset Y = 50 will roughly position the legend in the center of the canvas.
    /// More precisely, the top left corner of the legend itself will be pinned on that position.
    ///
    /// Note: you might want to resize the plot window to accomodate for the legend if you want it
    /// to be drawn outside the plot, otherwise it will be drawn on top of the plot figure.
    /// Check out [`BarPlot::set_plot_window_size`] for that.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// let apples: Vec<f64> = vec![5., 16., 17., 8., 3.];
    /// let oranges: Vec<f64> = vec![7., 6., 7., 16., 9.];
    ///
    /// // Add first set of values.
    /// plot.add_values(&apples);
    /// // Adding a second set of values.
    /// plot.add_values(&oranges);
    ///
    /// // First call adds a color to the first category. Red bacuse apples are often red.
    /// plot.add_bar_colors_by_category("Red");
    /// // Second call adds a color to the second category. Orange because; oranges are orange. :)
    /// plot.add_bar_colors_by_category("Orange");
    ///
    /// // Adding the labels (colors are already added) for legend.
    /// let categories = ["Apples", "Oranges"];
    /// // Applying the legend.
    /// plot.set_legend(&categories);
    ///
    /// // Make room for legend on the right side by shrinking plot window.
    /// plot.set_plot_window_size(80.0, 30.0, 85.0, 40.0);
    ///
    /// let offset_x = 90.0;
    /// let offset_y = 22.0;
    /// plot.set_legend_position(offset_x, offset_y);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_legend_position(&mut self, x: Percentage, y: Percentage) {
        self.legend.position = Some((x,y));
    }

    /// Apply a custom font-size for all text.
    ///
    /// By default, all text and numbers are rendered with a default font-size. This can be overriden.
    /// The font-size is calculated using a percentage value.
    /// A font-size of size 100 (100%) will not affect the text as it is the default.
    /// You can either increase the size by passing a >100 value or decrease it with <100.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.,]);
    ///
    /// // Scale down the plot figure size so text can become be visible.
    /// plot.set_plot_window_size(85.0, 65.0, 80.0, 40.0);
    ///
    /// // Apply some text.
    /// plot.set_text_left("This is some text.");
    ///
    /// // Increase font-size using a percentage greater than 100%.
    /// let size = 130.0;
    /// plot.set_font_size(size);
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_font_size(&mut self, p: Percentage) {
        self.layout.font_size = p;
    }

    /// Apply a border around the canvas.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Shrink plot so that it becomes clear that the border goes around the canvas.
    /// plot.set_plot_window_size(80.0, 30.0, 85.0, 40.0);
    ///
    /// plot.set_show_window_border();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_show_window_border(&mut self) {
        self.show.window_border = true;
    }

    /// Apply a border around the plot.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// // Shrink plot so that it becomes clear that the border goes around the plot window.
    /// plot.set_plot_window_size(80.0, 30.0, 85.0, 40.0);
    ///
    /// plot.set_show_plot_border();
    ///
    /// let svg: String = plot.to_svg(1600, 1000);
    /// ```
    pub fn set_show_plot_border(&mut self) {
        self.show.plot_border = true;
    }

    /// Generate the final svg image of all applied content.
    ///
    /// When you are satisfied with all the tweaks in above methods, it is time to generate the final svg.
    /// Keep in mind that you can still add or re-apply changes on the same object after calling this method.
    /// Changes will be applied and included on the following call to this method.
    ///
    /// # Example
    ///
    /// ```
    /// use eb_bars::BarPlot;
    ///
    /// let mut plot = BarPlot::new();
    ///
    /// plot.add_values(&[1., 2., 3.]);
    ///
    /// let mut svg: String = plot.to_svg(1600, 1000);
    ///
    /// // Add a new configuration (just resize plot window).
    /// plot.set_plot_window_size(80.0, 30.0, 85.0, 40.0);
    ///
    /// // Lets overwrite the old plot with this new configuration.
    /// svg = plot.to_svg(1600, 1000);
    /// ```
    pub fn to_svg(&mut self, width: u32, height: u32) -> String {
        assert!(!self.values.is_empty(), "Can not generate plot without any values..");

        self.size = (width, height);

        let n_categories = self.values.len();
        match &mut self.colors.bars.layout {
            BarColorLayout::Category(colors) => {
                let n_colors = colors.len();
                assert_eq!(
                    n_categories,
                    n_colors,
                    "Got {n_categories} categories and {n_colors} colors.",
                );
            }
            BarColorLayout::Indexed(matrix) => {
                let n_color_vectors = matrix.len();
                assert_eq!(
                    n_categories,
                    n_color_vectors,
                    "Got {n_categories} categories and {n_color_vectors} color vectors.",
                );

                for (i, colors) in matrix.iter().enumerate() {
                    let values = self.values[i].len();
                    let n_colors = colors.len();
                    assert_eq!(
                        values,
                        n_colors,
                        "Got {values} values and {n_colors} colors for category {i}.",
                    );
                }
            }
            // No need to do assertion on remaining variants..
            _ => (),
        }

        svg::render(self)
    }
}

#[cfg(test)]
mod tests {
    // All other tests are in directory ./tests

    use std::fs;

    use toml;

    use super::{VERSION, REPOSITORY};

    #[test]
    fn version_and_repo() {
        // When updating Cargo.toml, make sure to update corresponding values in src files as well.
        let contents = fs::read_to_string("Cargo.toml").unwrap();
        let value = contents.parse::<toml::Table>().unwrap();

        let version = value["package"]["version"].as_str().unwrap();
        assert_eq!(version, VERSION);

        let repository = value["package"]["repository"].as_str().unwrap();
        assert_eq!(repository, REPOSITORY);
    }
}