Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I generated this bar chart from JSON data using D3JS, but now I want to make it responsive..
how to do that, here's my code:

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Bar Chart | Salary</h2>

<script src="http://d3js.org/d3.v3.min.js" type="text/javascript"></script>
<style>
</style>
    <div id="test-chart"></div>
    <script>
        var json =  [
                { "EmpID": 1, "Salary": 21 },
                { "EmpID": 2, "Salary": 12 },
                { "EmpID": 3, "Salary": 11 },
                { "EmpID": 4, "Salary": 15 },
                { "EmpID": 5, "Salary": 14 },
                { "EmpID": 6, "Salary": 19 },
                { "EmpID": 7, "Salary": 16}]
        var w = 645;
        var h = 300;
        var x = d3.scale.linear()
    .domain([0, d3.max(json, function (d) { return d.Salary; })])
    .range([0, w]);
        var y = d3.scale.ordinal()
    .domain(d3.range(json.length))
    .rangeBands([0, h], 0.1);

        // Definig color Property
        var color = d3.scale.ordinal()
                .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

        // Adding SVG canvas
        var svg = d3.select("#test-chart")
    .append("svg")
    .attr("width", w)
    .attr("height", h)
    .append("g")
    .attr("transform", "translate(20,0)");

    // for Values rep
        var bars = svg.selectAll(".bar")
    .data(json)
    .enter().append("g")
    .attr("class", "bar")
    .attr("transform", function (d, i) { return "translate(" + 0 + "," + y(i) + ")"; });

     bars.append("rect")
    .attr("fill", function (d, i) { return color(i); })
    .attr("width", function (d) { return x(d.Salary); })
    .attr("height", y.rangeBand());

       bars.append("text")
      .attr("x", function (d) { return x(d.Salary); })
      .attr("y", 0 + y.rangeBand() / 2)
      .attr("dx", -18)
      .attr("dy", ".35em")
      .attr("text-anchor", "end")
      .text(function (d) { return d.Salary; });

    </script>

</asp:Content>
Posted
Comments
Abhishek Jaiswall 20-Aug-14 5:48am    
I tried this but its not displaying anything (neither chart nor its responsiveness)

<script src="http://d3js.org/d3.v3.min.js" type="text/javascript"></script>
<div id="chart-container"></div>
<link rel="Stylesheet" href="../../Content/Chart-Container.css" />

<script>
$(function(){
var json = [
{ "EmpID": 1, "Salary": 21 },
{ "EmpID": 2, "Salary": 12 },
{ "EmpID": 3, "Salary": 11 },
{ "EmpID": 4, "Salary": 15 },
{ "EmpID": 5, "Salary": 14 },
{ "EmpID": 6, "Salary": 19 },
{ "EmpID": 7, "Salary": 16}]

var $container = $('.chart-container'),
width = $container.width(),
height = $container.height(),
x = d3.scale.linear()
.domain([0, d3.max(json, function (d) { return d.Salary; })])
.range([0, width]);
y = d3.scale.ordinal().domain(d3.range(json.length))
.rangeBands([0, height], 0.1);

svg = d3.select('.chart-container')
.append("svg")
.attr("width", '100%')
.attr("height", '100%')
.attr('viewBox', '0 0 ')
.attr('preserveAspectRatio', 'xMinYMin')
.append("g")
.attr("transform", "translate(20,0)");

// Definig color Property
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

// for Values rep
var bars = svg.selectAll(".bar")
.data(json)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function (d, i) { return "translate(" + 0 + "," + y(i) + ")"; });

bars.append("rect")
.attr("fill", function (d, i) { return color(i); })
.attr("width", function (d) { return x(d.Salary); })
.attr("height", y.rangeBand());

bars.append("text")
.attr("x", function (d) { return x(d.Salary); })
.attr("y", 0 + y.rangeBand() / 2)
.attr("dx", -18)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function (d) { return d.Salary; });
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900