Skip to content

Commit

Permalink
Merge pull request #1366 from evidence-dev/echarts-options
Browse files Browse the repository at this point in the history
Add echarts custom options
  • Loading branch information
archiewood authored Dec 1, 2023
2 parents 1e1e7e3 + 9da524d commit 9bbb2d5
Show file tree
Hide file tree
Showing 30 changed files with 359 additions and 25 deletions.
12 changes: 11 additions & 1 deletion packages/component-utilities/src/echarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export default (node, option) => {
const chart = init(node, 'evidence-light', { renderer: useSvg ? 'svg' : 'canvas' });

chart.setOption(option);
// Check if echartsOptions are provided and apply them
const prevOption = chart.getOption();
if (prevOption.echartsOptions) {
chart.setOption(prevOption.echartsOptions);
}

const dispatch = option.dispatch;
chart.on('click', function (params) {
Expand Down Expand Up @@ -85,7 +90,12 @@ export default (node, option) => {

return {
update(option) {
chart.setOption(option, true, true);
chart.setOption(option);
// Check if echartsOptions are provided and apply them
const prevOption = chart.getOption();
if (prevOption.echartsOptions) {
chart.setOption(prevOption.echartsOptions);
}
updateLabelWidths();
},
destroy() {
Expand Down
5 changes: 5 additions & 0 deletions packages/component-utilities/src/echartsCanvasDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export default (node, option) => {
option.animation = false;

chart.setOption(option);
// Check if echartsOptions are provided and apply them
const prevOption = chart.getOption();
if (prevOption.echartsOptions) {
chart.setOption(prevOption.echartsOptions);
}

let src = chart.getConnectedDataURL({
type: 'png',
Expand Down
6 changes: 5 additions & 1 deletion packages/component-utilities/src/echartsCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { evidenceThemeLight } from './echartsThemes';
export default (node, option) => {
registerTheme('evidence-light', evidenceThemeLight);

const { config, ratio } = option;
const { config, ratio, echartsOptions } = option;

const chart = init(node, 'evidence-light', { renderer: 'canvas' });
config.animation = false; // disable animation

chart.setOption(config);
// Check if echartsOptions are provided and apply them
if (echartsOptions) {
chart.setOption(echartsOptions);
}

let src = chart.getConnectedDataURL({
type: 'jpeg',
Expand Down
8 changes: 8 additions & 0 deletions packages/component-utilities/src/echartsMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default (node, option) => {
const chart = init(node, 'none', { renderer: 'svg' });

chart.setOption(option.config);
// Check if echartsOptions are provided and apply them
if (option.echartsOptions) {
chart.setOption(option.echartsOptions);
}

let resizeObserver;
const containerElement = document.querySelector('div.content > article');
Expand Down Expand Up @@ -38,6 +42,10 @@ export default (node, option) => {
return {
update(option) {
chart.setOption(option.config, true, true);
// Check if echartsOptions are provided and apply them
if (option.echartsOptions) {
chart.setOption(option.echartsOptions);
}
},
destroy() {
if (resizeObserver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
export let labelColor = undefined;
export let labelFmt = undefined;
export let showAllLabels = undefined;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
</script>

<Chart
Expand Down Expand Up @@ -96,6 +99,8 @@
{stacked100}
{chartAreaHeight}
{colorPalette}
{echartsOptions}
{printEchartsConfig}
>
<Area
{line}
Expand Down
6 changes: 4 additions & 2 deletions packages/core-components/src/lib/unsorted/viz/BarChart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
export let stackTotalLabel = undefined;
export let showAllLabels = undefined;
export let options = undefined;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
</script>
<Chart
Expand Down Expand Up @@ -118,6 +119,8 @@
{chartAreaHeight}
{showAllXAxisLabels}
{colorPalette}
{echartsOptions}
{printEchartsConfig}
>
<Bar
{type}
Expand All @@ -132,7 +135,6 @@
{labelFmt}
{stackTotalLabel}
{showAllLabels}
{options}
/>
<slot />
</Chart>
5 changes: 5 additions & 0 deletions packages/core-components/src/lib/unsorted/viz/BoxPlot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
export let yMax = undefined;
export let swapXY = false;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
$: {
if (swapXY === 'true' || swapXY === true) {
swapXY = true;
Expand Down Expand Up @@ -89,6 +92,8 @@
{title}
{subtitle}
chartType="Box Plot"
{echartsOptions}
{printEchartsConfig}
>
<Box {boxPlotData} {color} {min} {max} />
<slot />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
let useTooltip = true;
export let colorPalette = undefined;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
</script>

<Chart
Expand Down Expand Up @@ -88,6 +90,8 @@
{sort}
{chartAreaHeight}
{colorPalette}
{echartsOptions}
{printEchartsConfig}
>
<Bubble {shape} {fillColor} {opacity} {outlineColor} {outlineWidth} {scaleTo} {useTooltip} />
<slot />
Expand Down
21 changes: 14 additions & 7 deletions packages/core-components/src/lib/unsorted/viz/Chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
export let showAllXAxisLabels = false;
export let printEchartsConfig = false; // helper for custom chart development
printEchartsConfig = printEchartsConfig === 'true' || printEchartsConfig === true;
// This should be reworked to fit better with svelte's reactivity.
// We rewrite the x and y values with fallbacks if they aren't present
Expand Down Expand Up @@ -135,7 +138,7 @@
export let legend = undefined;
// Additional Config Options:
export let options = undefined; // additional ECharts config object that will append to the config generated by our API
export let echartsOptions = undefined; // additional ECharts config object that will append to the config generated by our API
export let stackType = undefined; // used in BarChart (stacked, grouped) and AreaChart (stacked)
export let stacked100 = false;
Expand Down Expand Up @@ -980,10 +983,6 @@
color: colorPalette
};
if (options) {
chartConfig = { ...chartConfig, ...options };
}
config.update(() => {
return chartConfig;
});
Expand All @@ -999,13 +998,21 @@
});
}
}
$: data;
</script>
{#if !error}
<slot />
<ECharts config={$config} {height} {width} {data} {showAllXAxisLabels} {swapXY} />
<ECharts
config={$config}
{height}
{width}
{data}
{showAllXAxisLabels}
{swapXY}
{echartsOptions}
{printEchartsConfig}
/>
{:else}
<ErrorChart {error} {chartType} />
{/if}
16 changes: 13 additions & 3 deletions packages/core-components/src/lib/unsorted/viz/ECharts.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import echartsCanvasDownload from '@evidence-dev/component-utilities/echartsCanvasDownload';
import EchartsCopyTarget from './EchartsCopyTarget.svelte';
import DownloadData from '../ui/DownloadData.svelte';
import CodeBlock from '../ui/CodeBlock.svelte';
import ChartLoading from '../ui/ChartLoading.svelte';
import { flush } from 'svelte/internal';
import { createEventDispatcher } from 'svelte';
Expand All @@ -19,6 +20,9 @@
export let data;
export let echartsOptions = undefined;
export let printEchartsConfig; // helper for custom chart development
const dispatch = createEventDispatcher();
let downloadChart = false;
Expand Down Expand Up @@ -61,12 +65,12 @@
overflow: visible;
display: {copying ? 'none' : 'inherit'}
"
use:echarts={{ ...config, ...$$restProps, dispatch }}
use:echarts={{ ...config, ...$$restProps, echartsOptions, dispatch }}
/>
{/if}
{/if}

<EchartsCopyTarget {config} {height} {width} {copying} {printing} />
<EchartsCopyTarget {config} {height} {width} {copying} {printing} {echartsOptions} />

<div class="chart-footer">
<DownloadData
Expand Down Expand Up @@ -99,6 +103,12 @@
<DownloadData text="Download data" {data} class="download-button" display={hovering} />
{/if}
</div>

{#if printEchartsConfig && !printing}
<CodeBlock>
{JSON.stringify(config, undefined, 3)}
</CodeBlock>
{/if}
</div>

{#if downloadChart}
Expand All @@ -114,7 +124,7 @@
margin-bottom: 15px;
overflow: visible;
"
use:echartsCanvasDownload={config}
use:echartsCanvasDownload={{ ...config, ...$$restProps, echartsOptions }}
/>
{/if}

Expand Down
16 changes: 13 additions & 3 deletions packages/core-components/src/lib/unsorted/viz/EChartsMap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import echartsCanvasDownload from '@evidence-dev/component-utilities/echartsCanvasDownload';
import EchartsCopyTarget from './EchartsCopyTarget.svelte';
import DownloadData from '../ui/DownloadData.svelte';
import CodeBlock from '../ui/CodeBlock.svelte';
import ChartLoading from '../ui/ChartLoading.svelte';
import { flush } from 'svelte/internal';
Expand All @@ -20,6 +21,9 @@
export let hasLink = false;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
let downloadChart = false;
let copying = false;
let printing = false;
Expand Down Expand Up @@ -60,12 +64,12 @@
overflow: visible;
display: {copying ? 'none' : 'inherit'}
"
use:echartsMap={{ config, hasLink }}
use:echartsMap={{ config, hasLink, echartsOptions }}
/>
{/if}
{/if}

<EchartsCopyTarget {config} {height} {width} {copying} {printing} />
<EchartsCopyTarget {config} {height} {width} {copying} {printing} {echartsOptions} />

<div class="chart-footer">
<DownloadData
Expand Down Expand Up @@ -98,6 +102,12 @@
<DownloadData text="Download data" {data} class="download-button" display={hovering} />
{/if}
</div>

{#if printEchartsConfig && !printing}
<CodeBlock>
{JSON.stringify(config, undefined, 3)}
</CodeBlock>
{/if}
</div>

{#if downloadChart}
Expand All @@ -113,7 +123,7 @@
margin-bottom: 15px;
overflow: visible;
"
use:echartsCanvasDownload={config}
use:echartsCanvasDownload={{ ...config, echartsOptions }}
/>
{/if}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
export let width = '100%';
export let copying = false;
export let printing = false;
export let echartsOptions = undefined;
</script>

{#if copying}
Expand All @@ -24,7 +25,7 @@
overflow: visible;
break-inside: avoid;
"
use:eChartsCopy={{ config, ratio: 2 }}
use:eChartsCopy={{ config, ratio: 2, echartsOptions }}
/>
{:else if printing}
<div
Expand All @@ -38,6 +39,6 @@
overflow: visible;
break-inside: avoid;
"
use:eChartsCopy={{ config, ratio: 4 }}
use:eChartsCopy={{ config, ratio: 4, echartsOptions }}
/>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
export let funnelSort = 'none';
export let colorPalette = undefined;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
export let showPercent = false;
showPercent = showPercent === 'true' || showPercent === true;
Expand Down Expand Up @@ -202,4 +204,4 @@
};
</script>

<ECharts {config} {width} {height} />
<ECharts {config} {width} {height} {echartsOptions} {printEchartsConfig} />
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
export let fillOpacity = undefined;
export let colorPalette = undefined;
export let echartsOptions = undefined;
export let printEchartsConfig = false;
</script>

<Chart
Expand All @@ -57,6 +59,8 @@
hist="true"
{chartAreaHeight}
{colorPalette}
{echartsOptions}
{printEchartsConfig}
>
<Hist {fillColor} {fillOpacity} />
<slot />
Expand Down
Loading

0 comments on commit 9bbb2d5

Please sign in to comment.