我正在使用js中的小饼图可视化,并使用谷歌图表sdk进行同样的工作.

一切正常,但当我切片馅饼和旋转馅饼的开始Angular 时,切片的馅饼不是3D的

预期图表:

enter image description here

将馅饼切片并旋转的谷歌图表如下所示

enter image description here

我需要得到切片的馅饼,就像上面的图像中的3D切片馅饼(绿色)以及整个拼图的蓝色背景

为同样的目的附上了狙击手

如果还需要其他东西来解决这个问题,请告知

<html>
  <head>
    <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 data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     75],
          ['Eat',      25],
        ]);

        var options = {
          title: 'My Daily Activities',
          is3D: true,
          slices: {  1: {offset: 0.2},                   
          },
          pieStartAngle: 100,
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
  </body>
</html>

推荐答案

i found that if you reverse the order of the rows,
you can get the 3d effect to display properly.

随着切片的颠倒,我已经手动提供了与默认 colored颜色 匹配的 colored颜色 .

唯一的另一个区别是图例条目的顺序.

请参见下面的工作片段...

<html>
  <head>
    <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 data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Eat',     25],
          ['Work',      75],
        ]);

        var options = {
          colors: ['dc3912', '3366cc'],
          title: 'My Daily Activities',
          is3D: true,
          slices: {
            1: {offset: 0.2},                   
          },
          pieStartAngle: 12
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
  </body>
</html>

EDIT

as for adding a background color to the chart area,
I wasn't able to get config option chartArea.backgroundColor to work.

就我所知,对于饼图,整个图表就是图表区域,不同于其他图表.

我能想到的唯一其他 Select 是在图表的Ready事件上添加自定义图表元素.

finding the exact placement of the background element will be tricky.
I wasn't able to come up with anything quickly, I've left some hints on how that might be accomplished.
in the following example, the background element's placement is hard-coded, using a <circle> element.
but you could use any other element, such as <rect>, or <path>...

google.charts.load('current', {
  packages: ['corechart']
}).then(function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Task', 'Hours per Day'],
    ['Eat',     25],
    ['Work',      75],
  ]);

  var options = {
    colors: ['dc3912', '3366cc'],
    title: 'My Daily Activities',
    is3D: true,
    slices: {
      1: {offset: 0.2},
    },
    pieStartAngle: 12
  };

  var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));

  // add element to chart to use as background color
  function addBG() {
    var bg;
    var chartElements;
    var svg;
    var svgNS;

    // initialize chart elements
    svg = null;
    svgNS = null;
    chartElements = chart.getContainer().getElementsByTagName('svg');
    if (chartElements.length > 0) {
      svg = chartElements[0];
      svgNS = svg.namespaceURI;
    }
    chartElements = chart.getContainer().getElementsByTagName('path');

/*
    // calculate placement
    var x = null;
    var y = null;
    var height = null;
    var width = null;
    Array.prototype.forEach.call(chartElements, function(element, index) {
      var bounds = element.getBBox();
      x = x ?? bounds.x;
      x = Math.min(bounds.x, x);
      y = y ?? bounds.y;
      y = Math.min(bounds.y, y);
      height = height ?? bounds.height;
      height = Math.max(bounds.height, height);
      width = width ?? bounds.width;
      width = Math.max(bounds.width, width);
    });

    console.log('x', x);
    console.log('y', y);
    console.log('height', height);
    console.log('width', width);
*/

    // add background
    // <circle fill="rgba(255, 0, 255, 0.5)" cx="328" cy="260" r="180"></circle>
    bg = document.createElementNS(svgNS, 'circle');
    bg.setAttribute('fill', 'rgba(255, 0, 255, 0.5)');
    bg.setAttribute('cx', 328);
    bg.setAttribute('cy', 260);
    bg.setAttribute('r', 180);
    svg.insertBefore(bg, chartElements[0].parentNode);
  }

  google.visualization.events.addListener(chart, 'ready', function () {
    // add background element
    addBG();
  });

  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart_3d" style="width: 900px; height: 500px;"></div>

Javascript相关问答推荐

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

在Vite React库中添加子模块路径

google docs boldText直到按行执行应用脚本错误

用JavaScript复制C#CRC 32生成器

为什么按钮会随浮动属性一起移动?

我在我的Java代码中遇到了问题,代码的一部分看不到先前定义的对象

如何在 cypress 中使用静态嵌套循环

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

JS—删除对象数组中对象的子对象

当id匹配时对属性值求和并使用JavaScript返回结果

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

本地库中的chartjs-4.4.2和chartjs-plugin-注解

有条件重定向到移动子域

当我try 将值更改为True时,按钮不会锁定

用于部分字符串的JavaScript数组搜索

Socket.IO在刷新页面时执行函数两次

在单击按钮时生成多个表单时的处理状态

检测带有委托的元素内部的点击,以及元素何时按其类名被选中