0

so i want to use d3.js library and use a chart. i want to display how many projects has been opened in one year, and to show each month the sum of projects that have opened.

i saw this code:

d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/connectedscatter.csv",
...*GOING ON MORE*...

but i dont want to read data from csv, i want to read it from my DB and send it to the View. myabe in a ViewBag..

im attaching the source code: https://www.d3-graph-gallery.com/graph/connectedscatter_basic.html

i would love to know if theres a way that i could send to d3 the data in a different way and not from csv, thanks !

3
  • do you want to get data from controller as CSV using EF Commented Oct 29, 2019 at 18:06
  • You would normally send the data as JSON. See eg: learn.microsoft.com/en-us/visualstudio/get-started/csharp/… Commented Oct 29, 2019 at 21:45
  • You could get data from database by using EF Core and return data to View by using d3.json. Commented Oct 30, 2019 at 7:23

1 Answer 1

1

You could use d3.json(url,function(data){}) to return data from the controller to the view.

Here is a simple demo like below:

1.Model:

public class TestModels
{
    public int Id { get; set; }
    public string date { get; set; }
    public double value { get; set; }
}

2.View:

<div id="my_dataviz"></div>

@section Scripts{
    @*<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.6.0/d3.min.js"></script>*@
    <script src="https://d3js.org/d3.v4.js"></script>
    <script>
        // set the dimensions and margins of the graph
        var margin = { top: 10, right: 30, bottom: 30, left: 60 },
            width = 460 - margin.left - margin.right,
            height = 400 - margin.top - margin.bottom;
        // append the svg object to the body of the page
        var 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 + ")");
        //Read the data
        d3.json("Home/Test", 
             // Now I can use this dataset:
            function (data) {
                console.log(data);
                // Add X axis --> it is a date format
                var x = d3.scaleTime()
                    .domain(d3.extent(data, function (d) { return d3.timeParse("%Y-%m-%d")(d.date) }))
                    .range([0, width]);
                console.log(x);

                svg.append("g")
                    .attr("transform", "translate(0," + height + ")")
                    .call(d3.axisBottom(x));
                // Add Y axis
                var y = d3.scaleLinear()
                    .domain([8000, 9200])
                    .range([height, 0]);
                svg.append("g")
                    .call(d3.axisLeft(y));
                // Add the line
                svg.append("path")
                    .datum(data)
                    .attr("fill", "none")
                    .attr("stroke", "#69b3a2")
                    .attr("stroke-width", 1.5)
                    .attr("d", d3.line()
                        .x(function (d) { return x(d3.timeParse("%Y-%m-%d")(d.date)) })
                        .y(function (d) { return y(d.value) })
                    )
                // Add the points
                svg
                    .append("g")
                    .selectAll("dot")
                    .data(data)
                    .enter()
                    .append("circle")
                    .attr("cx", function (d) { return x(d3.timeParse("%Y-%m-%d")(d.date)) })
                    .attr("cy", function (d) { return y(d.value) })
                    .attr("r", 5)
                    .attr("fill", "#69b3a2")         
});
    </script>
}

3.Controller:

public class HomeController : Controller
{
    private readonly MyContext _context;
    public HomeController(MyContext context)
    {
        _context = context;
    }
    public IActionResult Index()
    {
        return View();
    }
    public IActionResult Test()
    {
        var data = new JsonResult(_context.TestModels.ToList());
        return data;
    }
}

4.Result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.