pub fn figure() -> FigureExpand description
Create a new figure with default settings
Examples found in repository?
examples/demo.rs (line 12)
3fn main() {
4 // Create some sample data
5 let x: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();
6 let y1: Vec<f64> = x.iter().map(|&x| x.sin()).collect();
7 let y2: Vec<f64> = x.iter().map(|&x| x.cos()).collect();
8 let y3: Vec<f64> = x.iter().map(|&x| (x * 0.5).sin() * 0.5).collect();
9
10 // Example 1: Simple line plot
11 println!("Creating simple line plot...");
12 let mut fig1 = figure();
13 fig1.add_subplot()
14 .plot(&x, &y1)
15 .set_title("Sine Function")
16 .set_xlabel("x")
17 .set_ylabel("sin(x)");
18
19 let svg_content = fig1.to_svg();
20 println!("Sine plot SVG content:\n{}", svg_content);
21
22 // Example 2: Multi-line plot
23 println!("Creating multi-line plot...");
24 let mut fig2 = figure_with_size(900.0, 600.0);
25 fig2.add_subplot()
26 .plot(&x, &y1)
27 .plot(&x, &y2)
28 .plot(&x, &y3)
29 .set_title("Trigonometric Functions Comparison")
30 .set_xlabel("x")
31 .set_ylabel("y")
32 .legend(true);
33
34 let svg_content = fig2.to_svg();
35 println!("Multi-line plot SVG content:\n{}", svg_content);
36
37 // Example 3: Scatter plot
38 println!("Creating scatter plot...");
39 let scatter_x: Vec<f64> = (0..50).map(|_| rand::random::<f64>() * 10.0).collect();
40 let scatter_y: Vec<f64> = scatter_x.iter().map(|&x| x * 2.0 + rand::random::<f64>() * 5.0).collect();
41
42 let mut fig3 = figure();
43 fig3.add_subplot()
44 .scatter(&scatter_x, &scatter_y)
45 .set_title("Random Scatter Plot")
46 .set_xlabel("X Value")
47 .set_ylabel("Y Value");
48
49 let svg_content = fig3.to_svg();
50 println!("Scatter plot SVG content:\n{}", svg_content);
51
52 // Example 4: Bar chart
53 println!("Creating bar chart...");
54 let categories: Vec<f64> = (1..=5).map(|i| i as f64).collect();
55 let values = vec![23.0, 45.0, 56.0, 78.0, 32.0];
56
57 let mut fig4 = figure();
58 fig4.add_subplot()
59 .bar(&categories, &values)
60 .set_title("Sales Data")
61 .set_xlabel("Month")
62 .set_ylabel("Sales Amount");
63
64 let svg_content = fig4.to_svg();
65 println!("Bar chart SVG content:\n{}", svg_content);
66
67 // Example 5: Graph from DOT markup
68 println!("Creating graph from DOT markup...");
69 let dot_content = r#"
70 digraph G {
71 A -> B;
72 B -> C;
73 C -> D;
74 D -> A;
75 A -> C;
76 }
77 "#;
78
79 match Figure::from_dot(dot_content) {
80 Ok(fig5) => {
81 let svg_content = fig5.to_svg();
82 println!("DOT graph SVG content:\n{}", svg_content);
83 }
84 Err(e) => {
85 println!("Error parsing DOT content: {}", e);
86 }
87 }
88
89 // Example 6: Chart with disabled axes
90 println!("Creating chart with disabled axes...");
91 let mut fig6 = figure();
92 fig6.add_subplot()
93 .plot(&x, &y1)
94 .set_title("Chart without X and Y axes")
95 .show_x_axis(false)
96 .show_y_axis(false);
97
98 let svg_content = fig6.to_svg();
99 println!("Chart without axes SVG content:\n{}", svg_content);
100
101 println!("All charts SVG content generated successfully!");
102
103 // Example 7: Combined chart with sine/cosine and DOT graph
104 println!("Creating combined chart with sine/cosine and DOT graph...");
105 let mut fig7 = figure();
106
107 // First subplot: sine and cosine functions
108 fig7.add_subplot()
109 .plot(&x, &y1)
110 .plot(&x, &y2)
111 .set_title("Sine and Cosine Functions")
112 .set_xlabel("X")
113 .set_ylabel("Y");
114
115 // Second subplot: DOT graph without axes
116 let dot_content = "digraph G {\n A -> B;\n B -> C;\n C -> D;\n A -> D;\n}";
117 println!("DOT content: {}", dot_content);
118 match fig7.add_dot_subplot(dot_content) {
119 Ok(dot_axes) => {
120 println!("DOT subplot added successfully with {} plots", dot_axes.plots.len());
121 dot_axes.set_title("DOT Graph (No Axes)")
122 .show_x_axis(false)
123 .show_y_axis(false);
124 }
125 Err(e) => println!("Error creating DOT graph: {}", e),
126 }
127
128 println!("Figure has {} subplots total", fig7.subplots.len());
129
130 let combined_svg = fig7.to_svg();
131 println!("Combined chart SVG content:\n{}", combined_svg);
132
133 // Save combined chart to file
134 std::fs::write("output/combined_chart.svg", &combined_svg).expect("Failed to write combined chart to file");
135 println!("Combined chart saved to combined_chart.svg");
136
137 // Example 8: Chart without background grid
138 println!("Creating chart without background grid...");
139 let mut fig8 = figure();
140
141 fig8.add_subplot()
142 .plot(&x, &y1)
143 .plot(&x, &y2)
144 .set_title("Sine and Cosine Functions (No Grid)")
145 .set_xlabel("X")
146 .set_ylabel("Y")
147 .grid(false); // Disable background grid
148
149 let no_grid_svg = fig8.to_svg();
150 println!("No grid chart SVG content:\n{}", no_grid_svg);
151
152 // Save no grid chart to file
153 std::fs::write("output/no_grid_chart.svg", &no_grid_svg).expect("Failed to write no grid chart to file");
154 println!("No grid chart saved to no_grid_chart.svg");
155
156 // Example 9: Advanced DOT graph layouts
157 println!("Creating advanced DOT graphs with different layouts...");
158
159 // Complex DOT graph for testing with subgraphs and new shapes
160 let complex_dot = r#"digraph G {
161
162 subgraph cluster_0 {
163 style=filled;
164 color=lightgrey;
165 node [style=filled,color=white];
166 a0 -> a1 -> a2 -> a3;
167 label = "process #1";
168 }
169
170 subgraph cluster_1 {
171 node [style=filled];
172 b0 -> b1 -> b2 -> b3;
173 label = "process #2";
174 color=blue
175 }
176 start -> a0;
177 start -> b0;
178 a1 -> b3;
179 b2 -> a3;
180 a3 -> a0;
181 a3 -> end;
182 b3 -> end;
183
184 start [shape=Mdiamond];
185 end [shape=Msquare];
186 }"#;
187
188 // Hierarchical layout
189 let mut fig9 = figure();
190 match fig9.add_dot_subplot_with_layout(complex_dot, plotiron::dot_renderer::LayoutAlgorithm::Hierarchical) {
191 Ok(axes) => {
192 axes.set_title("Hierarchical Layout")
193 .show_x_axis(false)
194 .show_y_axis(false)
195 .grid(false);
196 }
197 Err(e) => println!("Error creating hierarchical DOT graph: {}", e),
198 }
199
200 // Force-directed layout
201 match fig9.add_dot_subplot_with_layout(complex_dot, plotiron::dot_renderer::LayoutAlgorithm::ForceDirected) {
202 Ok(axes) => {
203 axes.set_title("Force-Directed Layout")
204 .show_x_axis(false)
205 .show_y_axis(false)
206 .grid(false);
207 }
208 Err(e) => println!("Error creating force-directed DOT graph: {}", e),
209 }
210
211 // Circular layout
212 match fig9.add_dot_subplot_with_layout(complex_dot, plotiron::dot_renderer::LayoutAlgorithm::Circular) {
213 Ok(axes) => {
214 axes.set_title("Circular Layout")
215 .show_x_axis(false)
216 .show_y_axis(false)
217 .grid(false);
218 }
219 Err(e) => println!("Error creating circular DOT graph: {}", e),
220 }
221
222 // Grid layout
223 match fig9.add_dot_subplot_with_layout(complex_dot, plotiron::dot_renderer::LayoutAlgorithm::Grid) {
224 Ok(axes) => {
225 axes.set_title("Grid Layout")
226 .show_x_axis(false)
227 .show_y_axis(false)
228 .grid(false);
229 }
230 Err(e) => println!("Error creating grid DOT graph: {}", e),
231 }
232
233 let advanced_dot_svg = fig9.to_svg();
234 std::fs::write("output/advanced_dot_layouts.svg", &advanced_dot_svg).expect("Failed to write advanced DOT layouts to file");
235 println!("Advanced DOT layouts saved to advanced_dot_layouts.svg");
236
237 println!("PlotIron - Rust plotting library demo completed");
238 println!("Generated SVG content for all chart types including advanced DOT graphs with multiple layouts, axis control, combined charts, and grid control");
239}