2amigos / yii2-chartjs-widget

ChartJs Widget For Yii2
https://2amigos.us
Other
108 stars 69 forks source link

Accessing Chart from another script #56

Closed timipoky closed 1 year ago

timipoky commented 1 year ago

Hello, is it possible to access the Chart from different script?

I would like to add checkboxes to show/hide datasets in multiple dataset pie.

I can see that in registerClientScript() there is initialization of the Chart chartJS_{$id} = new Chart($('#{$id}'),{$config});, I can see in my app, that it is filled with chartJS_w3, but when I try to use it I get an error Uncaught ReferenceError: chartJS_w3 is not defined.

This is my code:

<script>
        function updateChart(data) {
            console.log(chartJS_w3);
            const isDataShown = chartJS_w3.isDatasetVisible(data.value);
            if (isDataShown === true) {
                chartJS_w3.hide(data.value);
            }
            if (isDataShown === false) {
                chartJS_w3.show(data.value);
            }
        }
</script>

I found something similar to what I need at different package

You can access the JavaScript chart object from another script like this: var chart = $('#my-chart-id').highcharts();

timipoky commented 1 year ago

With some trial and error I made it work with

      updateChart = function (data) {
          const isDataShown = chartJS_w3.isDatasetVisible(data.value);
          if (isDataShown === true) {
              chartJS_w3.data.datasets[data.value].hidden = true;
              chartJS_w3.update();
          }
          if (isDataShown === false) {
              chartJS_w3.data.datasets[data.value].hidden = false;
              chartJS_w3.update();
          }
      }