put Subtitle on Chart.js

Asked

Viewed 502 times

0

I am working on a chart, and I am trying to put a Subtitle that varies according to the value that the user chooses. I didn’t find in any doc how to do this, and I tried to use Subtitle:{text:}, but it doesn’t work, and it doesn’t work. Does anyone know of any alternatives? My code below:

function chart_graph(){

  var ctx = document.getElementsByClassName("bar-chart","line-chart");

  var chartGraph = new Chart(ctx, { 
                type: 'bar', 
                data: { 
                  labels: dataBar1 , 
                datasets: [{ type: 'line',
                label:"Dias de Estoque", 
                data: dataBar2,
                yAxisID: "y-axis-0",
                backgroundColor: "rgba(250,250,250,0.0)",
                borderColor:     "rgba(255,99,132,0.9)",
                borderWidth:  4,
                },
                {
                label:"Valor de Estoque", 
                data: dataBar4,
                yAxisID: "y-axis-1",
                backgroundColor: "rgba(151,187,205,0.9)",
                },
                ] },
                options: {
                  legend: {
                    position: 'bottom'
                  },
                  title: {
                    display: true,
                    text: 'DIAS E VALOR(R$) EM ESTOQUE', 
                    subtitle: {
                    display: true,
                    text: subtitle_graph,  

                  },
                  },
                tooltips: {
                mode: 'label'
                },
                responsive: true,
                scales: {
                yAxes: [
                  {
                    gridLines:{

                      lineWidth:4,
                      zeroLineWidth : 4,
                              },
                stacked: true,
                position: "right",
                yAxisID: "y-axis-0",
                ticks: { min: 0, beginAtZero: true, 
                       },
                scaleLabel: {
                            display: true,
                            labelString: 'DIAS',
                               },
                },
                 {
                stacked: false,
                position: "left",
                yAxisID: "y-axis-1", 
                ticks: { min: 0, 
                         beginAtZero: true,
                         crosshair:{
                          enabled: true,
                          valueFormatString: "#,##0.##",

                      },
                 },
                scaleLabel: {
                            display: true,
                            labelString: 'VALOR(R$)',

                               },          
                  }    ]
                        }
                          }   
                             });
                 };

1 answer

1


You can pass a string array to the property title.text, as noted in official documentation:

var options = {
  type: 'pie',
  data: {
    labels: ["Opção 1", "Opção 2", "Opção 3"],
    datasets: [
        {
          data: [12, 19, 3]
        }
        ]
  },
  options: {
    title: {
        display: true,
        position: 'top',
        text: ['Título','Subtítulo'],
        fontSize: 14
    },
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);

Renderização do código exemplo


Another option is to use a plugin like Chart.js Subtitle Plugin, which allows greater flexibility than the previous option.

  • So I tried to do it this way(array)(text: ['DAYS AND VALUE IN STOCK',subtitle_graph],), and it looked like this: DAYS AND VALUE IN STOCK, MP-NAC It reads and prints exactly what it has within the array, including the comma :(

  • @Marlibenicio worked here, see: https://jsfiddle.net/qopkr0h2/. Make sure we are using the same version and the passage of the array is correct (in your comment o subtitle_graph is even a string already defined?).

  • I’m with this: "http://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js" So, yes. This variable(subtitle_graph), came from the controller(ruby on Rails), worked it(ajax) and brought it into Chart.js . I agree that in this way ['DAYS AND VALUE IN STOCK', subtitle_graph], it should work, but it’s understanding the comma as a string too and printing on the screen, and all in the same line. Is it by version account?

  • Yes! I read the changelog of version 2.7.0 (which my example uses) and it implemented support for multi-line titles.

  • Wow, thank you so much!! That’s right haha. I finally finished the task here. I was trying earlier versions and did not touch q could be a newer. :)

  • I know it comes out a little bit of the line of doubt posted, but I’m trying to add the values of each bar, fixed at the top of each one. I’m not finding any specific Puglin for this function... This option is possible on Chart.js?

  • Yes, there is an official plugin for this. Check out this answer: https://stackoverflow.com/q/31631354/3237900

  • It worked well, thanks again! :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.