|
1 | 1 | --- |
2 | 2 | title: Exporting Charts as SVG |
3 | | -page-title: How to export Vaadin Charts as SVG |
4 | | -description: How to export charts to SVG format. |
5 | | -meta-description: Create scalable and exportable SVG charts with Vaadin. Learn how to customize and integrate them into your applications effortlessly. |
6 | | -order: 9 |
| 3 | +section-nav: hidden |
7 | 4 | --- |
8 | 5 |
|
9 | | - |
10 | | -[[charts.svggenerator]] |
11 | 6 | = Exporting Charts as SVG |
12 | 7 |
|
13 | | -Charts can be exported as SVG by using the [classname]`SVGGenerator` class. This allows you to generate an SVG string of any chart, using a [classname]`Configuration` object with the chart's data. |
14 | | - |
15 | | -== Installing |
16 | | - |
17 | | -Add the `vaadin-charts-flow-svg-generator` dependency to your project's [filename]`pom.xml` as follows: |
18 | | - |
19 | | -[source,xml] |
20 | | ----- |
21 | | -<dependency> |
22 | | - <groupId>com.vaadin</groupId> |
23 | | - <artifactId>vaadin-charts-flow-svg-generator</artifactId> |
24 | | -</dependency> |
25 | | ----- |
26 | | - |
27 | | -.Node.js required |
28 | | -[NOTE] |
29 | | -Node.js must be installed for the generator to work. |
30 | | -See the configuration instructions in <<{articles}/flow/configuration/development-mode/node-js#,Installing Node.js>>. |
31 | | - |
32 | | -== Using the Generator |
33 | | - |
34 | | -Create an instance of [classname]`SVGGenerator` and call the [methodname]`generate()` method, passing the chart's [classname]`Configuration` as an argument. |
35 | | -The method returns a string with the SVG data of the chart. |
36 | | - |
37 | | -.Close the generator |
38 | | -WARNING: Remember to *close the generator when you're done using it*. |
39 | | -It's recommended to use a try-with-resources block, so the generator is automatically closed. |
40 | | - |
41 | | -[source,java] |
42 | | ----- |
43 | | -Configuration configuration = new Configuration(); |
44 | | -// ... |
45 | | -try (SVGGenerator generator = new SVGGenerator()) { |
46 | | - String svg = generator.generate(configuration); |
47 | | -} catch (IOException | InterruptedException ex) { |
48 | | - // handle exceptions accordingly |
49 | | -} |
50 | | ----- |
51 | | - |
52 | | -== Customizing SVG Generation |
53 | | - |
54 | | -You can customize some attributes of the resulting SVG. |
55 | | - |
56 | | -The customizable options include the following: |
57 | | - |
58 | | -* Width |
59 | | -* Height |
60 | | -* Theme |
61 | | -* Language |
62 | | -* Showing timeline |
63 | | -* Executing JavaScript code (formatter functions) |
64 | | - |
65 | | -Any customizations are handled with the [classname]`ExportOptions` class. |
66 | | - |
67 | | -.CSS styling not supported |
68 | | -NOTE: CSS styling isn't supported when exporting a chart as SVG. |
69 | | - |
70 | | -[source,java] |
71 | | ----- |
72 | | -Configuration configuration = new Configuration(); |
73 | | -// ... |
74 | | -ExportOptions options = new ExportOptions(); |
75 | | -options.setWidth(800); |
76 | | -options.setHeight(600); |
77 | | -try (SVGGenerator generator = new SVGGenerator()) { |
78 | | - String svg = generator.generate(configuration, options); |
79 | | -} catch (IOException | InterruptedException ex) { |
80 | | - // handle exceptions accordingly |
81 | | -} |
82 | | ----- |
83 | | - |
84 | | -=== Executing JavaScript Functions |
85 | | - |
86 | | -You can execute functions from [classname]`Configuration` objects, for example, formatter functions. |
87 | | -Executing functions must be explicitly enabled by setting the `executeFunctions` flag to `true`. |
88 | | - |
89 | | -.Only Run Trusted Code |
90 | | -CAUTION: Enabling this option allows JavaScript code to run on your server. |
91 | | -Make sure to only allow safe and trusted code. |
92 | | - |
93 | | -[source,java] |
94 | | ----- |
95 | | -Configuration configuration = new Configuration(); |
96 | | -configuration.getyAxis().getLabels().setFormatter("function () { return this.value +' mm'; }"); |
97 | | -// ... |
98 | | -ExportOptions options = new ExportOptions(); |
99 | | -options.setExecuteFunctions(true); |
100 | | -try (SVGGenerator generator = new SVGGenerator()) { |
101 | | - String svg = generator.generate(configuration, options); |
102 | | -} catch (IOException | InterruptedException ex) { |
103 | | - // handle exceptions accordingly |
104 | | -} |
105 | | ----- |
106 | | - |
107 | | -== Preview SVG (For Debugging) |
108 | | - |
109 | | -You can add the generated SVG to a component to see the resulting image. |
110 | | - |
111 | | -.For debugging purposes only |
112 | | -CAUTION: Use the <<basic-use#,Chart>> component to render charts. |
113 | | -This approach is only intended to help you debug your application. |
114 | | - |
115 | | -[source,java] |
116 | | ----- |
117 | | -Div div = new Div(); |
118 | | -Configuration configuration = new Configuration(); |
119 | | -// ... |
120 | | -try (SVGGenerator generator = new SVGGenerator()) { |
121 | | - String svg = generator.generate(configuration); |
122 | | - div.getElement().setProperty("innerHTML", svg); |
123 | | -} catch (IOException | InterruptedException ex) { |
124 | | - // handle exceptions accordingly |
125 | | -} |
126 | | -add(div); |
127 | | ----- |
128 | | - |
| 8 | +The contents of this page have been moved to <<{articles}/components/charts/exporting#, Exporting Charts>> page. |
129 | 9 |
|
130 | | -[discussion-id]`C8510402-621D-437D-ADD9-E219BBDB532D` |
0 commit comments