Wednesday, November 3, 2021

How to create x axis and y axis in d3js chart

 <!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title>D3JS</title>

    <script src="https://d3js.org/d3.v4.min.js" type="text/javascript"></script>

    <link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"

          type="text/css"/>

    <link 

          href="https://image.shutterstock.com/image-vector/internet-icon-black-computer-arrow-600w-1028095990.jpg" 

          rel="shortcut icon" />

</head>

<body>

    

        <p>D3 Tutorials</p>

    <script>

        var width = 400; height = 300;


        var data = [10, 15, 20, 25, 30];


        var svg = d3.select("body").append("svg").attr("width", width)

            .attr("height", height);


        var xScale = d3.scaleLinear().domain([0, d3.max(data)]).

            range([0, width - 100]);


        //or d3.min(data)

        var yScale = d3.scaleLinear().domain([0, d3.max(data)]).

            range([height/2, 0]);


        var y_axis = d3.axisLeft().

            scale(yScale);


        var x_axis = d3.axisBottom()

            .scale(xScale);


        svg.append("g").attr("transform", "translate(50, 10)")

            .call(y_axis);


        var xAxisTranslate = height / 2 + 10;


        svg.append("g").attr("transform", "translate(50, " + xAxisTranslate + ")").call(x_axis);


    </script>

    <input type="button" class="w3-button w3-black" value="Input Button">

</body>

</html>

No comments:

Post a Comment