|
1 |
| -import { Chart, ChartConfiguration, RadarControllerChartOptions, registerables } from 'chart.js'; |
| 1 | +import { Chart, ChartConfiguration, SankeyControllerDatasetOptions, registerables } from 'chart.js'; |
| 2 | +import { SankeyController, Flow } from 'chartjs-chart-sankey'; |
2 | 3 | import './date-adapter/chartjs-adapter-moment.esm.js';
|
3 | 4 | import { MarkdownPostProcessorContext, MarkdownRenderChild, parseYaml, TFile } from 'obsidian';
|
4 | 5 | import { generateInnerColors, renderError } from 'src/util';
|
5 |
| -import type { ChartPluginSettings, ImageOptions } from './constants/settingsConstants'; |
| 6 | +import type { ImageOptions } from './constants/settingsConstants'; |
6 | 7 | import type ChartPlugin from 'src/main';
|
7 | 8 | import { generateTableData } from 'src/chartFromTable';
|
8 | 9 | import annotationPlugin from 'chartjs-plugin-annotation'
|
9 | 10 |
|
10 |
| -Chart.register(...registerables, annotationPlugin); |
| 11 | +Chart.register(...registerables, annotationPlugin, SankeyController, Flow); |
11 | 12 |
|
12 | 13 | // I need to refactor this
|
13 | 14 | // Or just rewrite it completely
|
@@ -37,15 +38,26 @@ export default class Renderer {
|
37 | 38 | }
|
38 | 39 | }
|
39 | 40 | for (let i = 0; yaml.series.length > i; i++) {
|
40 |
| - datasets.push({ |
41 |
| - label: yaml.series[i].title ?? "", |
42 |
| - data: yaml.series[i].data, |
| 41 | + const {title, ...rest} = yaml.series[i]; |
| 42 | + const dataset = { |
| 43 | + label: title ?? "", |
43 | 44 | backgroundColor: yaml.labelColors ? colors.length ? generateInnerColors(colors, yaml.transparency) : generateInnerColors(this.plugin.settings.colors, yaml.transparency) : colors.length ? generateInnerColors(colors, yaml.transparency)[i] : generateInnerColors(this.plugin.settings.colors, yaml.transparency)[i],
|
44 | 45 | borderColor: yaml.labelColors ? colors.length ? colors : this.plugin.settings.colors : colors.length ? colors[i] : this.plugin.settings.colors[i],
|
45 | 46 | borderWidth: 1,
|
46 | 47 | fill: yaml.fill ? yaml.stacked ? i == 0 ? 'origin' : '-1' : true : false, //See https://github.com/phibr0/obsidian-charts/issues/53#issuecomment-1084869550
|
47 | 48 | tension: yaml.tension ?? 0,
|
48 |
| - }); |
| 49 | + ...rest, |
| 50 | + }; |
| 51 | + if (yaml.type === 'sankey') { |
| 52 | + // colorFrom, colorTo is accepted as object in yaml, but should be function for sankey. |
| 53 | + if (dataset.colorFrom) |
| 54 | + (dataset as SankeyControllerDatasetOptions).colorFrom = (c) => yaml.series[i].colorFrom[c.dataset.data[c.dataIndex].from] ?? colors[i] ?? 'green' |
| 55 | + |
| 56 | + if (dataset.colorTo) |
| 57 | + (dataset as SankeyControllerDatasetOptions).colorTo = (c) => yaml.series[i].colorTo[c.dataset.data[c.dataIndex].to] ?? colors[i] ?? 'green' |
| 58 | + |
| 59 | + } |
| 60 | + datasets.push(dataset); |
49 | 61 | }
|
50 | 62 | }
|
51 | 63 |
|
@@ -147,7 +159,34 @@ export default class Renderer {
|
147 | 159 | },
|
148 | 160 | }
|
149 | 161 | };
|
150 |
| - } else { |
| 162 | + } else if (yaml.type === 'sankey') { |
| 163 | + datasets = datasets.map(dataset => { |
| 164 | + return { |
| 165 | + ...dataset, |
| 166 | + data: dataset.data.map((item: object | any[]) => |
| 167 | + Array.isArray(item) && item.length === 3 ? |
| 168 | + { |
| 169 | + from: item[0], |
| 170 | + flow: item[1], |
| 171 | + to: item[2], |
| 172 | + } : item |
| 173 | + ) |
| 174 | + } |
| 175 | + }) as ChartConfiguration<'sankey'>['data']['datasets']; |
| 176 | + |
| 177 | + (chartOptions as ChartConfiguration<'sankey'>) = { |
| 178 | + type: yaml.type, |
| 179 | + data: { |
| 180 | + labels, |
| 181 | + datasets, |
| 182 | + }, |
| 183 | + options: { |
| 184 | + animation: { |
| 185 | + duration: 0 |
| 186 | + }, |
| 187 | + } |
| 188 | + } |
| 189 | + }else { |
151 | 190 | (chartOptions as ChartConfiguration<"pie" | "doughnut" | "bubble" | "scatter">) = {
|
152 | 191 | type: yaml.type,
|
153 | 192 | data: {
|
|
0 commit comments