Concepts
Export & imperative handle
Each chart exposes a handle with toSVGString() and getGeometry(). The exported SVG carries its own ground colours, so it renders the same outside the app.
Try it
| name | value | share |
|---|---|---|
| Rent | 38 | 38% |
| Food | 22 | 22% |
| Savings | 15 | 15% |
| Transport | 14 | 14% |
| Leisure | 11 | 11% |
In your code
import { useRef } from 'react';
import { LineChart, type ChartHandle } from '@silverpoint/react/line-chart';
export function Exportable({ data }: { data: { hour: string; hits: number }[] }) {
const chart = useRef<ChartHandle>(null);
const download = () => {
const svg = chart.current?.toSVGString() ?? '';
window.open(URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' })));
};
return (
<>
<LineChart ref={chart} data={data} xKey="hour" valueKey="hits" title="Hits per hour" />
<button onClick={download}>Export SVG</button>
</>
);
}<script setup lang="ts">
import { ref } from 'vue';
import { SpLineChart } from '@silverpoint/vue/line-chart';
defineProps<{ data: { hour: string; hits: number }[] }>();
const chart = ref<InstanceType<typeof SpLineChart>>();
function download() {
const svg = chart.value?.toSVGString() ?? '';
window.open(URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' })));
}
</script>
<template>
<SpLineChart ref="chart" :data="data" x-key="hour" value-key="hits" title="Hits per hour" />
<button @click="download">Export SVG</button>
</template>import { Component, viewChild } from '@angular/core';
import { SpLineChart } from '@silverpoint/angular/line-chart';
@Component({
selector: 'app-exportable',
imports: [SpLineChart],
template: `
<sp-line-chart #chart [data]="data" xKey="hour" valueKey="hits" title="Hits per hour" />
<button (click)="download()">Export SVG</button>
`,
})
export class Exportable {
protected readonly data = [
{ hour: '00', hits: 18 },
{ hour: '12', hits: 64 },
];
private readonly chart = viewChild.required<SpLineChart>('chart');
protected download() {
const svg = this.chart().toSVGString();
window.open(URL.createObjectURL(new Blob([svg], { type: 'image/svg+xml' })));
}
}from reflex_silverpoint_react import download_svg, get_geometry, get_svg, line_chart
line_chart(id="hits", data=DATA, x_key="hour", value_key="hits")
rx.button("Download SVG", on_click=download_svg("hits", "hits.svg")) # in the browser
rx.button("SVG to Python", on_click=get_svg("hits", State.receive_svg)) # handler gets a str
rx.button("Geometry", on_click=get_geometry("hits", State.receive_geometry)) # handler gets a dict