简体   繁体   English

谷歌图表未正确显示图例

[英]Google chart not displaying the legends correctly

Find the sample issue in the screenshot below.在下面的屏幕截图中找到示例问题。 When we load the graph UI using google charts, the legends data are coming but not displayed properly.当我们使用 google charts 加载图形 UI 时,图例数据即将到来但未正确显示。 The frequency of this issue is less in laptops but frequency is high in desktops.此问题在笔记本电脑中的频率较低,但在台式机中的频率较高。 Please guide.请指导。 在此处输入图片说明

this problem is the result of drawing the chart in a hidden container这个问题是在隐藏的容器中绘制图表的结果

to demonstrate, see following working snippet,要演示,请参阅以下工作片段,
the chart remains hidden while it is drawn,图表在绘制时保持隐藏状态,
then shown when the chart's 'ready' event is fired然后在图表的'ready'事件被触发时显示
the result is similar to the image posted结果与发布的图像相似

 google.charts.load('current', { packages: ['bar'] }).then(function () { var data = google.visualization.arrayToDataTable([ [ {label: 'Year', id: 'year', type: 'date'}, {label: 'Annual Sales', id: 'Sales', type: 'number'}, {label: 'Annual Expenses', id: 'Expenses', type: 'number'}, {label: 'Annual Profit', id: 'Profit', type: 'number'}], [{v:new Date('2014'), f:'2014'}, 0, 400, 200], [{v:new Date('2014'), f:'2014'}, 1000, 0, 0], [{v:new Date('2015'), f:'2015'}, 0, 460, 250], [{v:new Date('2015'), f:'2015'}, 1170, 0, 0], [{v:new Date('2016'), f:'2016'}, 0, 1120, 300], [{v:new Date('2016'), f:'2016'}, 600, 0, 0], [{v:new Date('2017'), f:'2017'}, 0, 540, 350], [{v:new Date('2017'), f:'2017'}, 1030, 0, 0] ]); var options = { chart: { title: 'Company Performance', subtitle: 'Sales, Expenses, and Profit: 2014-2017', }, bars: 'horizontal', hAxis: { format: 'decimal' }, vAxis: { format: 'yyyy' }, height: 400, colors: ['#1b9e77', '#d95f02', '#7570b3'], isStacked: true, bar: { groupWidth: '90%' } }; var container = document.getElementById('chart_div'); var chart = new google.charts.Bar(container); google.visualization.events.addListener(chart, 'ready', function () { $(container).removeClass('hidden'); }); chart.draw(data, google.charts.Bar.convertOptions(options)); });
 .hidden { display: none; visibility: hidden; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://www.gstatic.com/charts/loader.js"></script> <div class="hidden" id="chart_div"></div>


to correct this issue,为了纠正这个问题,
make sure the chart's container is visible,确保图表的容器可见,
before drawing for the first time在第一次画画之前
see following working snippet...请参阅以下工作片段...

 google.charts.load('current', { packages: ['bar'] }).then(function () { var data = google.visualization.arrayToDataTable([ [ {label: 'Year', id: 'year', type: 'date'}, {label: 'Annual Sales', id: 'Sales', type: 'number'}, {label: 'Annual Expenses', id: 'Expenses', type: 'number'}, {label: 'Annual Profit', id: 'Profit', type: 'number'}], [{v:new Date('2014'), f:'2014'}, 0, 400, 200], [{v:new Date('2014'), f:'2014'}, 1000, 0, 0], [{v:new Date('2015'), f:'2015'}, 0, 460, 250], [{v:new Date('2015'), f:'2015'}, 1170, 0, 0], [{v:new Date('2016'), f:'2016'}, 0, 1120, 300], [{v:new Date('2016'), f:'2016'}, 600, 0, 0], [{v:new Date('2017'), f:'2017'}, 0, 540, 350], [{v:new Date('2017'), f:'2017'}, 1030, 0, 0] ]); var options = { chart: { title: 'Company Performance', subtitle: 'Sales, Expenses, and Profit: 2014-2017', }, bars: 'horizontal', hAxis: { format: 'decimal' }, vAxis: { format: 'yyyy' }, height: 400, colors: ['#1b9e77', '#d95f02', '#7570b3'], isStacked: true, bar: { groupWidth: '90%' } }; var container = document.getElementById('chart_div'); var chart = new google.charts.Bar(container); chart.draw(data, google.charts.Bar.convertOptions(options)); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

从编辑图表“数据”选项卡中选择使用“使用第 1 行作为标题”

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

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