1

I want to create a line chart for the following data in powerbi custom visual d3.js

Student name Marks
Jasmine 45
Oliver 33
Maya 11
Ethan 66
Sophia 77
Lucas 8
Ava 88
Clara 11

I've tried follwing code , but I'm getting a blank visual.Are there any latest resources for creating visuals in d3.js.

var margin = {top: 50, right: 50, bottom: 50, left: 50};

// Calculate the width and height of the chart area
var width = pbi.width - margin.left - margin.right;
var height = pbi.height - margin.top - margin.bottom;

// Define x and y scales
var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], 0.1, 0.2);

var y = d3.scale.linear()
    .range([height, 0]);

var svg = d3.select("#chart")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g") // Append a 'g' element for grouping
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


pbi.dsv(type, function(Names) {
    // Set the domain for x and y scales based on data
    x.domain(Names.map(function(d) { return d.Name; }));
    y.domain([0, d3.max(Names, function(d) { return d.Marks; })]);
  
  
    svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

    // Append y axis
    svg.append("g")
        .attr("class", "y axis")
        .call(d3.axisLeft(y));

    // Append bars
    svg.selectAll(".bar")
        .data(data)
        .enter()
        .append("rect")
        .attr("class", "bar")
        .attr("x", function(d) { return x(d.Name); })
        .attr("width", x.bandwidth())
        .attr("y", function(d) { return y(d.Marks); })
        .attr("height", function(d) { return height - y(d.Marks); })
        .style("fill", pbi.colors[0]); 
 
  
});

function type(d) {
    d.Marks = +d.Marks;
    return d;
}
5
  • Why not the native line chart or Deneb if you need more customisation? Commented Jul 1, 2024 at 7:24
  • only d3.js is allowed to use in our organization. So trying in d3 itself Commented Jul 1, 2024 at 7:42
  • feeling it is bit complicated, and didn't found much resources online Commented Jul 1, 2024 at 7:42
  • That doesn;t maje sense. D3.js isn't certified whereas Deneb is which means not a security issue. Commented Jul 1, 2024 at 7:49
  • Even I don't have any idea about that why it is like that, that's why I'm trying in d3 only Commented Jul 2, 2024 at 11:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.