Widget:Chartjs bar: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
Tag: 2017 source edit
No edit summary
Tag: 2017 source edit
 
(19 intermediate revisions by the same user not shown)
Line 10: Line 10:
</noinclude>
</noinclude>
<includeonly>
<includeonly>
<!--{counter name="counterDonut" assign="countDonut"}-->
<!--{counter name="counterPie" assign="countPie"}-->
<!--{math equation='rand(0,1000)' assign="randomValue"}-->
<!--{math equation='rand(0,1000)' assign="randomValue"}-->
<!--{assign var=time value=time()}-->
<!--{assign var=time value=time()}-->
<!--{$ID = "donut-$countDonut-$time-$randomValue"}-->
<!--{$ID = "donut-$countPie-$time-$randomValue"}-->


<div>
<div class="chart-container" style="position: relative;">
<canvas id="myChart-1"></canvas>
  <canvas id="<!--{$ID|escape:'html'}-->"></canvas>
</div>
</div>
<script>
<script>
// defer script execution until the mediawiki loader and jQuery are available
// defer script execution until the mediawiki loader and jQuery are available
RLQ.push([[], function () {
RLQ.push([[], function () {
mw.loader.using('ext.iswiki.chartjs_doughnut').then(function(){
  mw.loader.using('ext.iswiki.chartjs').then(function(){
const ctx = document.getElementById('myChart-1');
    const ctx = document.getElementById('<!--{$ID|escape:'html'}-->');


new Chart(ctx, {
    function makeItemsArray(itemString) {
type: 'bar',
       let itemsArray = itemString.split(",");
    data: {
       for (var i = 0; i< itemsArray.length; i++) {
       labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
        itemsArray[i] = itemsArray[i].trim();
       datasets: [
        {
          label: "Population (millions)",
          backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
          data: [2478,5267,734,784,433]
        }
      ]
    },
    options: {
      legend: { display: false },
      title: {
        display: true,
        text: 'Predicted world population (millions) in 2050'
       }
       }
      return itemsArray;
     }
     }
})


    let labels = makeItemsArray('<!--{$data_labels|default:''}-->');
    let dataIdentifier = '<!--{$data_identifier|default:''}-->';
    let colors = makeItemsArray('<!--{$colors|default:'#36a2eb, #ff6384, #4bc0c0, #ff9f40, #9966ff, #ffcd56, #c9cbcf'}-->');
    let dataSeries = makeItemsArray('<!--{$data_series|default:''}-->');
    let title = '<!--{$title|default:''}-->';
    let legend = '<!--{$legend|default:'yes'}-->';
    let orientation = '<!--{$orientation|default:''}-->';


    dataIdentifier = dataIdentifier.trim();
    title = title.trim();
    if (legend === 'no') {
        legend = false;
    } else {
        legend = true;
    }
    if (orientation === 'horizontal') {
        indexAxis = 'y';
    } else {
        indexAxis = 'x';
    }
    function drawPieChart(ctx, labels, dataIdentifier, colors, dataSeries, indexAxis, legend, title) {
      //console.log('ctx: ' + ctx);
      //console.log('labels: ' + labels);
      //console.log('dataIdentifier: ' + dataIdentifier);
      //console.log('colors: ' + colors);
      //console.log('dataSeries: ' + dataSeries);
      //console.log('indexAxis: ' + indexAxis);
      console.log('legend: ' + legend);
      //console.log('title: ' + title);
 
      new Chart(ctx, {
        type: 'bar',
        data: {
          labels: labels,
          datasets: [{
            label: dataIdentifier,
            backgroundColor: colors,
            data: dataSeries
          }]
        },
        options: {
          //maintainAspectRatio: false,
          //responsive: true,
          indexAxis: indexAxis,
          title: {
            display: true,
            text: title
          },
          plugins: {
            legend: { display: false },
          } 
        }
      });
    }


    window.drawPieChart = drawPieChart(ctx, labels, dataIdentifier, colors, dataSeries, indexAxis, legend, title);


});
  });
}]);
}]);
</script>
</script>
</includeonly>
</includeonly>

Latest revision as of 13:52, 19 March 2024

This widget allows you to add ... widget to your wiki page.

Created by [idea-sketch.com idea-sketch.com]

Using this widget[edit source]

For information on how to use this widget, see ...

Copy to your site[edit source]

To use this widget on your site, just install MediaWiki Widgets extension and copy the full source code of this page to your wiki as page Widget:Chartjs bar.