【问题标题】:Concentric arcs in C3.jsC3.js 中的同心圆弧
【发布时间】:2016-08-17 14:38:31
【问题描述】:

我正在尝试在 C3.js 的圆环图中实现同心圆弧。到目前为止,我已经创建了圆环图,现在想在圆环内添加另一个弧线,表示篮球运动员三分球命中率的百分比。

我在使用 C3.js 时找不到任何此类示例。

图表如下所示: Donut Chart

我想添加另一个 15 像素大小的弧线,并覆盖三个指针的百分比。这是我到目前为止的代码。

var chart = c3.generate({
                        data: {
                            columns: [
                                ['Shots', 50],
                                ['Threes', 50]
                            ],
                            type: 'donut',
                            colors: {
                                Shots: '#ff0000',
                                Threes: '#E8E8EE'
                            }                               
                        },
                        donut: {
                              expand: false,
                              label: {
                                  show: false,
                                  format: function(value, ratio) {
                                        console.log("value: " + value)
                                        return value;
                                  }
                              },
                              width: 15
                        },
                        legend: {
                            hide: true
                        },
                        tooltip: {
                            show: false
                        }
                    });

                    d3.select(".c3-chart-arcs-title")
                    .append("tspan")
                    .attr("dy", -20)
                    .attr("x", 0)
                    .text("Year: 5");

                    d3.select(".c3-chart-arcs-title")
                    .append("tspan")
                    .attr("dy", 16)
                    .attr("x", 0)
                    .text("50% Shots Made");

                    d3.select(".c3-chart-arcs-title")
                    .append("tspan")
                    .attr("dy", 16)
                    .attr("x", 0)
                    .text("25% 3ptrs Made");

【问题讨论】:

    标签: javascript d3.js c3.js


    【解决方案1】:

    我放弃了使用 C3,因为我找不到可以使它工作的文档。这是在 D3.js 中制作两个同心圆弧的代码……这样更容易。

    <div id="chart"></div>
    
    <style>
                            .arc {
                                fill: #00b33c;
                            }
                            .arc2 {
                                fill: #d3d3d3;
                            }
    
                            .arc3 {
                                fill: #196719;
                            }                                                       
                        </style>
    
    <script type="text/javascript">
    
    var shotsMade = .63;
    var shotsMissed = 1 - shotsMade;
    var threePtrs = .50;
    
                        var svg = d3.select("#chart")
                            .append("svg")
                            .attr("width", 300)
                            .attr("height", 220)
                            .append("g")
                            .attr("transform", "translate(100,100)");
    
                        var arc = d3.svg.arc()
                            .innerRadius(90)
                            .outerRadius(100)
                            .startAngle(0)
                            .endAngle(shotsMade * 2 * Math.PI);
    
                        svg.append("path")
                            .attr("class", "arc")
                            .attr("d", arc);
    
                        var arc2 = d3.svg.arc()
                            .innerRadius(90)
                            .outerRadius(100)
                            .startAngle(0)
                            .endAngle(-1 * shotsMissed * 2 * Math.PI);
    
                        svg.append("path")
                            .attr("class", "arc2")
                            .attr("d", arc2);
    
                        var arc3 = d3.svg.arc()
                            .innerRadius(80)
                            .outerRadius(90)
                            .startAngle(0)
                            .endAngle(threePtrs * 2 * Math.PI);
    
                        svg.append("path")
                            .attr("class", "arc3")
                            .attr("d", arc3);
    
                        var textConfirmed = svg.append("text")
                        .attr("dy", "0")
                        .style("text-anchor", "middle")
                        .style('fill', '#196719')
                        .attr("class", "shots")
                        .text("50% Three Ptrs")
                        .attr("y", 12);
    
                        var textAdherence = svg.append("text")
                        .attr("dy", "0")
                        .style("text-anchor", "middle")
                        .style('fill', '#00b33c')
                        .attr("class", "threePtrs")
                        .text( "60% Shots Made" )
                        .attr("y", 28);
    
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      相关资源
      最近更新 更多