diff --git a/assets/javascripts/discourse/components/data-explorer-bar-chart.hbs b/assets/javascripts/discourse/components/data-explorer-bar-chart.hbs new file mode 100644 index 0000000..a5c2e6d --- /dev/null +++ b/assets/javascripts/discourse/components/data-explorer-bar-chart.hbs @@ -0,0 +1,4 @@ + diff --git a/assets/javascripts/discourse/components/data-explorer-bar-chart.js b/assets/javascripts/discourse/components/data-explorer-bar-chart.js index 6a74afc..0abb366 100644 --- a/assets/javascripts/discourse/components/data-explorer-bar-chart.js +++ b/assets/javascripts/discourse/components/data-explorer-bar-chart.js @@ -1,41 +1,42 @@ -import Component from "@ember/component"; +import Component from "@glimmer/component"; import loadScript from "discourse/lib/load-script"; -import discourseComputed from "discourse-common/utils/decorators"; import themeColor from "../lib/themeColor"; +import { bind } from "discourse-common/utils/decorators"; +import { action } from "@ember/object"; -export default Component.extend({ - barsColor: themeColor("--tertiary"), - barsHoverColor: themeColor("--tertiary-high"), - gridColor: themeColor("--primary-low"), - labelsColor: themeColor("--primary-medium"), - chart: null, +export default class DataExplorerBarChart extends Component { + chart; + barsColor = themeColor("--tertiary"); + barsHoverColor = themeColor("--tertiary-high"); + gridColor = themeColor("--primary-low"); + labelsColor = themeColor("--primary-medium"); - @discourseComputed("data", "options") - config(data, options) { + get config() { + const data = this.data; + const options = this.options; return { type: "bar", data, options, }; - }, + } - @discourseComputed("labels.[]", "values.[]", "datasetName") - data(labels, values, datasetName) { + get data() { + const labels = this.args.labels; return { labels, datasets: [ { - label: datasetName, - data: values, + label: this.args.datasetName, + data: this.args.values, backgroundColor: this.barsColor, hoverBackgroundColor: this.barsHoverColor, }, ], }; - }, + } - @discourseComputed - options() { + get options() { return { scales: { legend: { @@ -68,31 +69,23 @@ export default Component.extend({ ], }, }; - }, + } - didInsertElement() { - this._super(...arguments); - this._initChart(); - }, + @bind + async initChart(canvas) { + await loadScript("/javascripts/Chart.min.js"); + const context = canvas.getContext("2d"); + // eslint-disable-next-line + this.chart = new Chart(context, this.config); + } - didUpdate() { - this._super(...arguments); + @action + updateChartData() { this.chart.data = this.data; this.chart.update(); - }, + } - willDestroyElement() { - this._super(...arguments); + willDestroy() { this.chart.destroy(); - }, - - _initChart() { - loadScript("/javascripts/Chart.min.js").then(() => { - const canvas = this.element.querySelector("canvas"); - const context = canvas.getContext("2d"); - const config = this.config; - // eslint-disable-next-line - this.chart = new Chart(context, config); - }); - }, -}); + } +} diff --git a/assets/javascripts/discourse/templates/components/data-explorer-bar-chart.hbs b/assets/javascripts/discourse/templates/components/data-explorer-bar-chart.hbs deleted file mode 100644 index aa8cc32..0000000 --- a/assets/javascripts/discourse/templates/components/data-explorer-bar-chart.hbs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/assets/javascripts/discourse/templates/explorer-query-result.hbs b/assets/javascripts/discourse/templates/explorer-query-result.hbs index 71b120c..c390e44 100644 --- a/assets/javascripts/discourse/templates/explorer-query-result.hbs +++ b/assets/javascripts/discourse/templates/explorer-query-result.hbs @@ -52,11 +52,11 @@
{{#if showChart}} - {{data-explorer-bar-chart - labels=chartLabels - values=chartValues - datasetName=chartDatasetName - }} + {{else}}