使用NVd3创建半圆形量规

问题描述 投票:0回答:1

我有一个正在使用nvd3.js的项目,想要创建一个半圆形量规。如何创建它?

我确实尝试过使用guage.js,但是我面临的问题是我无法为字符串类型的量规添加自定义/标签标签,并且找不到解决该问题的方法。任何帮助表示赞赏。

nvd3.js jsgauge
1个回答
0
投票

这是我想出的。这并不完美,工具提示毫无意义。另外,我一直在尝试提供渐变填充以使其具有3D外观,但尚未成功。

   <div style="height: 200px; width: 350px;">
      <div style="height: 350px; width: 350px;">
         <svg id="gaugeContainer"></svg>
      </div>
   </div>
   <script>

    //d3.json("/api/gauge", updateChart);

    updateChart({ "max": 100, "current": 90 });

    function updateChart(data) {
        var containerSvg = "gaugeContainer";
        var height = 350;
        var width = 350;

        // The first graph section always starts at 0 degrees, but we
        // want it to be horizontal so we rotate the whole thing -90 
        // degrees, then correct the labels.

        // The formula for needle angle is ((current/max) * 180) - 90
        // current / max will always be 1.0 or less, 
        // the fraction of max that the needle indicates,
        // * 180 converts to degrees and -90 compensates for rotation.
        var indicatorAngle = ((data.current / data.max) * 180) - 90;

        var colors = ["green", "orange", "red", "white"];

        // Apparently after 4 sections are defined, the fifth one is 
        // dead, as in not graphed.  We play a little trick, defining a 
        // very small white section so we can display
        // the max value at the end of its visible range.
        // The key values could just as easily be strings.
        var graphData = [
            { key: Math.round((data.max / 8) * 1), y: 1 },
            { key: Math.round((data.max / 8) * 3), y: 1 },
            { key: Math.round((data.max / 8) * 6), y: 2 },
            { key: Math.round((data.max / 8) * 8), y: 0.2 },
            { key: "", y: 3.8 }
        ];

        var arcRadius = [
            { inner: 0.6, outer: 1 },
            { inner: 0.6, outer: 1 },
            { inner: 0.6, outer: 1 },
            { inner: 0.6, outer: 1 }
        ];

        nv.addGraph(function () {
            var chart = nv.models.pieChart()
                .x(function (d) { return d.key })
                .y(function (d) { return d.y })
                .donut(true)
                .showTooltipPercent(false)
                .width(width)
                .height(height)
                .color(colors)
                .arcsRadius(arcRadius)
                .donutLabelsOutside(true)
                .showLegend(false)
                .growOnHover(false)
                .labelSunbeamLayout(false);

            d3.select("#" + containerSvg)
                .datum(graphData)
                .transition().duration(1)
                .attr('width', width)
                .attr('height', height)
                .attr("transform", "rotate(-90)")
                .call(chart);
            // draw needle
            d3.select("#" + containerSvg)
                .append("path")
                .attr("class", "line")
                .attr("fill", "none")
                .style("stroke", "gray")
                .style("stroke-width", "1")
                .attr("d", "M0, -3 L153, 0 0,3 Z")
                .attr("transform", "translate(175,179) rotate(" + indicatorAngle + ")");

            d3.select("#" + containerSvg)
                .append("circle")
                .attr("r", "6")
                .attr("cx", "0")
                .attr("cy", "0")
                .style("stroke", "gray")
                .style("stroke-width", "1")
                .attr("transform", "translate(175,179) rotate(" + indicatorAngle + ")");
            // correct text rotation
            d3.select("#" + containerSvg).selectAll("text")
                .attr("transform", "rotate(90)");

            return chart;
        });
    }

</script>
© www.soinside.com 2019 - 2024. All rights reserved.