web-dev-qa-db-fra.com

Centrer le titre Google Chart

Comment centrer le titre dans un graphique à lignes à partir de l'API Google Charts (pas d'images Google Chart)

Je ne vois aucune option comme titlePosition: 'center'

Merci!

function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['xValues', 'yValues'],
    [0, 34.92],
    [389, 39.44],
    [488, 52.11],
    [652, 55.4]
  ]);

  var options = {
    curveType: 'none', // 'function'
    title: 'Title',
    titleTextStyle: {
      color: '333333',
      fontName: 'Arial',
      fontSize: 10
    },
    legend: 'none',
    enableInteractivity: false
  };

  var chart = new google.visualization.LineChart(document.getElementById('visualization'));
  chart.draw(data, options);
}

Un séjour sans faille

16
Turnercj65

Cette option n'est pas fournie par l'API ; vous ne pouvez simplement pas faire cela.

19

J'utilise: TitlePosition: 'none'

Et insérez un titre en HTML normal

10
Werner

Une autre réponse suggère quelque chose comme ce qui suit:

$("text:contains(" + vencOptions.title + ")").attr({'x':'60', 'y':'20'})

Source: https://stackoverflow.com/a/16291236/364

4
Matt Mitchell

Après avoir dessiné le graphique, appelez la fonction de gestionnaire d’événements:

google.visualization.events.addListener(chart, 'ready', Title_center);

Et définir la fonction comme:

function Title_center(){

     var title_chart=$("#payer_chart_div svg g").find('text').html();   

     $("#payer_chart_div svg").find('g:first').html('<text text-anchor="start" x="500" y="141" font-family="Arial" font-size="18" font-weight="bold" stroke="none" stroke-width="0" fill="#000000">'+title_chart+'</text>');
}

Insérez simplement le titre de votre graphique et ajoutez les attributs X:500, y:141

2
Anuja Saravanan

Vous pouvez facilement le faire avec les informations de rappel du graphique après qu’il a été dessiné et un peu de calcul en travaillant sur du texte dans une div.

Contient votre graphique dans une position: div relative. Ajoutez un div sous le graphique et positionnez-le ainsi que l'absolu du graphique.

Ensuite, vous pouvez déplacer les positions supérieure et gauche de la div avec un peu de maths au lycée.

<https://jsfiddle.net/o6zmbLvk/2/>
1
Ray Myers
function Title_center(total) {
    var title_chart = $("#chart svg g").find('text').html();
    var x = $("#chart svg g").find('rect').attr('width');
    var y = $("#chart svg g").find('rect').attr('y');
    alert(x);
    $("#chart svg").find('g:first')
                   .html('<text text-anchor="start" x="'+x/2+'" y="'+y+'" font-family="Arial" font-size="12" font-weight="bold" stroke="none" stroke-width="0" fill="#000000">' + total + '</text>');
}

Créez cette fonction dans un script et appelez les graphiques après:

google.visualization.events.addListener(chart, 'ready', Title_center("Test success"));
0
Gopal Singh
$("#YOUR_GRAPH_WRAPPER svg text").first()
   .attr("x", (($("#time-series-graph svg").width() - $("#YOUR_GRAPH_WRAPPER svg text").first().width()) / 2).toFixed(0));

Voir mon explication ici

0
tkhuynh
$("text:contains(" + vencOptions.title + ")").attr({'x':'60', 'y':'20'})

//OU

$("#YOUR_GRAPH_WRAPPER svg text").first().attr("x", (($("#time-series-graph svg").width() - parseInt($("#YOUR_GRAPH_WRAPPER svg text").first().attr('x'),10)) / 2).toFixed(0));
0
ravi chandra

Pour afficher le titre du graphique au centre du graphique, utilisez l’extrait de code ci-dessous; il définira dynamiquement le titre au centre du conteneur de graphique.

Ajouter ready écouteur d'événement; google.visualization.events.addListener(myChart, 'ready', titleCenter); et ajouter la fonction titleCenter.

google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawAxisTickColors);

var options = {
  title: "Chart Title",
  titleTextStyle: {
      bold: true,
      italic: true,
      fontSize: 18,
  },
  width: 600,
  height: 400,
  legend: { position: 'top', maxLines: 3 },
  bar: { groupWidth: '75%' },
  isStacked: true,
};

function drawAxisTickColors() {
    var data = google.visualization.arrayToDataTable([
      ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
       'Western', 'Literature', { role: 'annotation' } ],
      ['2010', 10, 24, 20, 32, 18, 5, ''],
      ['2020', 16, 22, 23, 30, 16, 9, ''],
      ['2030', 28, 19, 29, 30, 12, 13, '']
    ]);
    
    var myChart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    google.visualization.events.addListener(myChart, 'ready', titleCenter);
    myChart.draw(data, options);
}

function titleCenter() {
    var $container = $('#chart_div');
    var svgWidth = $container.find('svg').width();
    var $titleElem = $container.find("text:contains(" + options.title + ")");
    var titleWidth = $titleElem.html().length * ($titleElem.attr('font-size')/2);
    var xAxisAlign = (svgWidth - titleWidth)/2;
    $titleElem.attr('x', xAxisAlign);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

0