概览
差异图表是一种图表,旨在突出显示两个具有类似数据的图表之间的差异。通过突出显示类似值之间的变化,它们可以揭示数据集之间的差异。
如需创建差异图表,您可以使用两个数据集调用 computeDiff
方法,以生成表示差异的第三个数据集,然后绘制该数据集。
示例
在本部分中,您将看到每种差异图表类型的示例和代码示例。
差异散点图
为了演示差异散点图,我们假设有一对实验,每个实验比较了两种药物。以下为两个实验的结果以及生成的差异图表:
如果您观察这两个图表,就会发现其中有些方面发生了变化,但很难准确判断具体是什么。差异散点图表通过显示每个数据点的轨迹来清楚说明这一点。
以下是生成上述三个图表的关键行:
// Create the three charts: before, after, and diff. var chartBefore = new google.visualization.ScatterChart(document.getElementById('chart_before_div')); var chartAfter = new google.visualization.ScatterChart(document.getElementById('chart_after_div')); var chartDiff = new google.visualization.ScatterChart(document.getElementById('chart_diff_div')); // Draw the before and after charts. chartBefore.draw(oldData); chartAfter.draw(newData); // Draw the diff chart. var diffData = chartDiff.computeDiff(oldData, newData); chartDiff.draw(diffData);
您可以使用 diff.oldData.opacity
选项更改尾部的不透明度,并使用 diff.newData.opacity
选项更改新数据点的不透明度:
差异饼图
以下是生成所有三个图表的代码:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {packages:['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var oldData = google.visualization.arrayToDataTable([ ['Major', 'Degrees'], ['Business', 256070], ['Education', 108034], ['Social Sciences & History', 127101], ['Health', 81863], ['Psychology', 74194]]); var newData = google.visualization.arrayToDataTable([ ['Major', 'Degrees'], ['Business', 358293], ['Education', 101265], ['Social Sciences & History', 172780], ['Health', 129634], ['Psychology', 97216]]); var options = { pieSliceText: 'none' }; var chartBefore = new google.visualization.PieChart(document.getElementById('piechart_before')); var chartAfter = new google.visualization.PieChart(document.getElementById('piechart_after')); var chartDiff = new google.visualization.PieChart(document.getElementById('piechart_diff')); chartBefore.draw(oldData, options); chartAfter.draw(newData, options); var diffData = chartDiff.computeDiff(oldData, newData); chartDiff.draw(diffData, options); } </script> <span id='piechart_before' style='width: 450px; display: inline-block'></span> <span id='piechart_after' style='width: 450px; display: inline-block'></span> <br> <span id='piechart_diff' style='width: 450px; position: absolute; left: 250px'></span>
您可以使用 diff.innerCircle.radiusFactor
控制圆形的相对大小,使用 diff.innerCircle.borderFactor
控制圆形之间的边框,使用 diff.oldData.opacity
和 diff.newData.opacity
控制每个圆形的透明度。最后,您可以反转该行为,以便使用 diff.oldData.inCenter
将最新数据包含在最旧的数据周围。各选项的示例如下:
差异条形图和柱形图
差异条形图和差异柱形图会在旧数据之上叠加新数据。在这里,我们选取两个简单的柱形图(一个是旧数据,另一个是新数据),然后进行差异比较:
举一个更复杂的示例,我们来看看从 20 世纪 80 年代至今,某些名字(尤其是 Google 可视化图表夏季实习生的名字)的受欢迎程度如何变化(该数字为千百万婴儿,由婴儿名字向导提供)。
用于生成全部四个图表的代码:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {packages:['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var oldData = google.visualization.arrayToDataTable([ ['Name', 'Popularity'], ['Cesar', 250], ['Rachel', 4200], ['Patrick', 2900], ['Eric', 8200] ]); var newData = google.visualization.arrayToDataTable([ ['Name', 'Popularity'], ['Cesar', 370], ['Rachel', 600], ['Patrick', 700], ['Eric', 1500] ]); var colChartBefore = new google.visualization.ColumnChart(document.getElementById('colchart_before')); var colChartAfter = new google.visualization.ColumnChart(document.getElementById('colchart_after')); var colChartDiff = new google.visualization.ColumnChart(document.getElementById('colchart_diff')); var barChartDiff = new google.visualization.BarChart(document.getElementById('barchart_diff')); var options = { legend: { position: 'top' } }; colChartBefore.draw(oldData, options); colChartAfter.draw(newData, options); var diffData = colChartDiff.computeDiff(oldData, newData); colChartDiff.draw(diffData, options); barChartDiff.draw(diffData, options); } </script> <span id='colchart_before' style='width: 450px; height: 250px; display: inline-block'></span> <span id='colchart_after' style='width: 450px; height: 250px; display: inline-block'></span> <span id='colchart_diff' style='width: 450px; height: 250px; display: inline-block'></span> <span id='barchart_diff' style='width: 450px; height: 250px; display: inline-block'></span>
通过差异列和条形图,您可以使用 diff.newData.widthFactor
选项控制新旧条形的相对宽度: