简体   繁体   English

Google Charts:饼图标题位置

[英]Google Charts: Pie Chart title position

I was assigned to implement some Charts, and the bosses requested me to separate the title of the chart from the Chart, I tried to move the chart's area a little from the top, but the title moved with the chart too, like this: 我被指派实施一些图表,并且老板要求我将图表的标题与图表分开,我试图将图表的区域从顶部稍微移开,但标题也随图表移动,如下所示:

在chartArea之前在chartArea之后

I tried using: chartArea:{top:80} and the result was that, on the screenshot. 我尝试使用: chartArea:{top:80} ,结果就是截图。 I'm sure the property to move only the title it's another but I can't find it yet. 我确定该物业只移动标题是另一个,但我还没找到它。 btw, This is my whole Object: 顺便说一句,这是我的整个对象:

var columnChartProps = {
    fontName: 'HelveticaNeueBC',
    legend: {position: 'none'},
    titleTextStyle: {fontSize: 18},
    hAxis:  {color: '#121b2d'},
    vAxis: {
        gridlines: {color: '#666666'}
    },
    width: 400,
    height: 300,
    backgroundColor: '#CBCBCB',
    chartArea:{top:80, width:"80%"}
}
// VENCIMIENTOS:
var vencData = google.visualization.arrayToDataTable([
    ['equis', 'Vencimientos'],
    ['ENE', 7],
    ['FEB', 10],
    ['MAR', 5],
    ['ABR', 6],
    ['MAY', 10],
    ['JUN', 15]
]);

var vencOptions = {
    colors:['#24375e','#2c4370','#324a7c','#38538a','#3e5b96','#4363a3'],
    title: 'Vencimientos prox. 6 meses',
    hAxis: {title: 'Meses'}
};

var vencimientos = new google.visualization.ColumnChart(document.getElementById('vencimientos'));
vencimientos.draw(vencData, jQuery.extend({},vencOptions,columnChartProps));

Thanks in advance :) 提前致谢 :)

I don't think you can do this through options in the vis api. 我不认为你可以通过vis api中的选项来做到这一点。

However.... 然而....

The title is held in a text element that uses x/y positioning 标题保存在使用x / y定位的文本元素中

If you are using JQuery, this becomes a piece of cake 如果您使用的是JQuery,这将变得轻而易举

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

Adding that immediately after the .draw call and tweaking the x and y coords gives you pixel-perfect control. 在.draw调用之后立即添加,并调整x和y坐标,可以实现像素完美控制。

You can do it without JQuery, but time is pressing and I'll leave that to someone else :) 你可以在没有JQuery的情况下做到这一点,但时间紧迫,我会把它留给别人:)

as PerryW said, I don't think you can do this with the given API. 正如PerryW所说,我认为你不能用给定的API做到这一点。 The easiest way to do this would be to make an html table with the chart on the second row. 最简单的方法是在第二行创建一个带有图表的html表。

<table>
    <thead>
        <th>Vencimientos prox. 6 meses</th>
    </thead>
    <tbody>
        <td>
            ***YOUR CHART HERE***
        </td>
    </tbody>
</table>

Now you can play around with the style and location as much as you want! 现在,您可以根据需要随意使用风格和位置!

It's annoyed when Google did provide title position but with jQuery, you can do it 谷歌确实提供了标题位置时很烦,但是使用jQuery,你可以做到

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

Basically, you want to access the first text tag which is your chart title and change value of x attribute. 基本上,您希望访问第一个文本标记,即您的图表标题和更改x属性的值。 To center it, take svg width (your graph width) subtract by title widtch, then divide by 2 :) Happy hacking :) 以它为中心,取svg宽度(你的图形宽度)减去标题widtch,然后除以2 :)快乐黑客:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM