【问题标题】:highlighting the max valued bar突出显示最大值栏
【发布时间】:2021-10-04 19:21:39
【问题描述】:

我创建了一个条形图 我想要的只是如何获得最大栏的亮点

问题 问题是我必须突出显示最大值的栏 我使用 d3v6 创建图表

下面是我用来创建栏的当前代码

<!DOCTYPE html>
<meta charset="utf-8">
          
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
          
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
      
<script>

// set the dimensions and margins of the graph
const margin = {top: 30, right: 30, bottom: 70, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
  .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", `translate(${margin.left},${margin.top})`);

// Parse the Data
//d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum_header.csv").then( function(data) {

// X axis
final_data=
{
"total":10,
"plotting_data":[
{slider: "01", Value: "3"},
{slider: "02", Value: "5"},
{slider: "03", Value: "6"},
{slider: "04", Value: "4"},
{slider: "05", Value: "5"},



]
}
data=final_data["plotting_data"]

console.log("data:-",data)
const x = d3.scaleBand()
  .range([ 0, width ])
  .domain(data.map(d => d.slider))
  .padding(0.2);
svg.append("g")
  .attr("transform", `translate(0, ${height})`)
  .call(d3.axisBottom(x).tickSize(0))
  .selectAll("text")
    .attr("transform", "translate(0,0)rotate(0)") //rotate(0) it can be used to tilt the text
    .style("text-anchor", "center");//here we can specify the start end and center values

// Add Y axis
const y = d3.scaleLinear()
  .domain([0, final_data['total']])
  .range([ height, 0]);
svg.append("g")
  .call(d3.axisLeft(y).tickSize(0));

const rx =5;
const ry =5;

// Bars
var bars = svg.selectAll("mybar")
  .data(data)
  .enter()
  .append("g")
  
bars.append("path")
    .attr("x", d => x(d.slider))
    .attr("y", d => y(d.Value))
    .attr("width", x.bandwidth())
    .attr("height", d => height - y(d.Value))
    .attr("fill", "#056DFF")
    .attr(
        "d",
        item => `
        M${x(item.slider)},${y(item.Value) + ry}
        a${rx},${ry} 0 0 1 ${rx},${-ry}
        h${x.bandwidth() - 2 * rx}
        a${rx},${ry} 0 0 1 ${rx},${ry}
        v${height - y(item.Value) - ry}
        h${-x.bandwidth()}Z
      `
      )
    
bars.append("text")
            .attr("class", "label")
            //y position of the label is halfway down the bar
            .attr("y",  function (d) { 
            if (d.Value>=5/100*final_data['total']){
            
             return y(d.Value)+10;
            }
            else 
            {return y(d.Value)};
            })
            //x position is 3 pixels to the right of the bar
            .attr("x", d => x(d.slider) +x.bandwidth()/2)
            .style("font-size", function (d) {
                
                return 10;
            })
            .style("font-family","sans-serif")
            .style('fill', 'white')
            
            .text(function (d) {
                return d.Value;
            })
            .style("text-anchor", "middle");





  svg.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 0 - margin.left)
      .attr("x",0 - (height / 2))
      .attr("dy", "1em")
      .style("text-anchor", "middle")
      .text("No of resources");      




  // text label for the x axis
  svg.append("text")             
      .attr("transform",
            "translate(" + (width/2) + " ," + 
                           (height + margin.top + 20) + ")")
      .style("text-anchor", "middle")
      .text("Slider Value")
      .style('fill', 'black');
</script>
</body>

电流输出

预期输出

任何帮助或指导都会有很大帮助 对于下面的问题评论有任何疑问

【问题讨论】:

    标签: javascript html css d3.js


    【解决方案1】:

    这是使用您的代码的解决方案。它添加一行来查找最大值,然后如果它与最大值匹配,则添加不同的填充。所以最大栏是红色的。

    // set the dimensions and margins of the graph
    const margin = {top: 30, right: 30, bottom: 70, left: 60},
        width = 460 - margin.left - margin.right,
        height = 400 - margin.top - margin.bottom;
    
    // append the svg object to the body of the page
    const svg = d3.select("#my_dataviz")
      .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform", `translate(${margin.left},${margin.top})`);
    
    // Parse the Data
    //d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum_header.csv").then( function(data) {
    
    // X axis
    final_data=
    {
    "total":10,
    "plotting_data":[
    {slider: "01", Value: "3"},
    {slider: "02", Value: "5"},
    {slider: "03", Value: "6"},
    {slider: "04", Value: "4"},
    {slider: "05", Value: "5"},
    
    
    
    ]
    }
    data=final_data["plotting_data"]
    
    // Added this to find max
    max = d3.max(data.map(d => d.Value))
    
    console.log("data:-",data)
    const x = d3.scaleBand()
      .range([ 0, width ])
      .domain(data.map(d => d.slider))
      .padding(0.2);
    svg.append("g")
      .attr("transform", `translate(0, ${height})`)
      .call(d3.axisBottom(x).tickSize(0))
      .selectAll("text")
        .attr("transform", "translate(0,0)rotate(0)") //rotate(0) it can be used to tilt the text
        .style("text-anchor", "center");//here we can specify the start end and center values
    
    // Add Y axis
    const y = d3.scaleLinear()
      .domain([0, final_data['total']])
      .range([ height, 0]);
    svg.append("g")
      .call(d3.axisLeft(y).tickSize(0));
    
    const rx =5;
    const ry =5;
    
    // Bars
    var bars = svg.selectAll("mybar")
      .data(data)
      .enter()
      .append("g")
      
    bars.append("path")
        .attr("x", d => x(d.slider))
        .attr("y", d => y(d.Value))
        .attr("width", x.bandwidth())
        .attr("height", d => height - y(d.Value))
        // Set the fill based on if its max
        .attr("fill", d => d.Value === max ? '#FF0000FF' :"#056DFF")
        .attr(
            "d",
            item => `
            M${x(item.slider)},${y(item.Value) + ry}
            a${rx},${ry} 0 0 1 ${rx},${-ry}
            h${x.bandwidth() - 2 * rx}
            a${rx},${ry} 0 0 1 ${rx},${ry}
            v${height - y(item.Value) - ry}
            h${-x.bandwidth()}Z
          `
          )
        
    bars.append("text")
                .attr("class", "label")
                //y position of the label is halfway down the bar
                .attr("y",  function (d) { 
                if (d.Value>=5/100*final_data['total']){
                
                 return y(d.Value)+10;
                }
                else 
                {return y(d.Value)};
                })
                //x position is 3 pixels to the right of the bar
                .attr("x", d => x(d.slider) +x.bandwidth()/2)
                .style("font-size", function (d) {
                    
                    return 10;
                })
                .style("font-family","sans-serif")
                .style('fill', 'white')
                
                .text(function (d) {
                    return d.Value;
                })
                .style("text-anchor", "middle");
    
    
    
    
    
      svg.append("text")
          .attr("transform", "rotate(-90)")
          .attr("y", 0 - margin.left)
          .attr("x",0 - (height / 2))
          .attr("dy", "1em")
          .style("text-anchor", "middle")
          .text("No of resources");      
    
    
    
    
      // text label for the x axis
      svg.append("text")             
          .attr("transform",
                "translate(" + (width/2) + " ," + 
                               (height + margin.top + 20) + ")")
          .style("text-anchor", "middle")
          .text("Slider Value")
          .style('fill', 'black');
    <script src="https://d3js.org/d3.v6.js"></script>
    
    <div id="my_dataviz">

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2022-01-11
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      相关资源
      最近更新 更多