【问题标题】:Adding tooltip and x axis, y axis title to D3.js chart向 D3.js 图表添加工具提示和 x 轴、y 轴标题
【发布时间】:2016-02-01 09:47:06
【问题描述】:

我想在圆、X 轴和 Y 轴标题中添加带有 Y 轴值和时间信息的工具提示。我试图将代码放在 svg.selectAll 中,但未能获得图形显示本身。请帮我解决这个问题

var app = angular.module('myApp', []);
app.controller('myController', ['$scope', function ($scope) {
    //setting the data in the scope
    $scope.data = [{
        "date": "2012-04-30T18:30:00.000Z",
            "close": 58.13,
            "ews" : 3,
    }, {
        "date": "2012-04-29T18:30:00.000Z",
            "close": 53.98,
            "ews" : 3
    }, {
        "date": "2012-04-26T18:30:00.000Z",
            "close": 67,
            "ews" : 3
    },  {
        "date": "2012-04-24T18:30:00.000Z",
            "close": 99,
            "ews" : 2
    }, {
        "date": "2012-04-23T18:30:00.000Z",
            "close": 80,
            "ews" : 3
    }, {
        "date": "2012-04-22T18:30:00.000Z",
            "close": 167,
            "ews" : 0
    }, {
        "date": "2012-04-19T18:30:00.000Z",
            "close": 234,
            "ews" : 3
    }, {
        "date": "2012-04-18T18:30:00.000Z",
            "close": 101,
            "ews" : 1
    }, {
        "date": "2012-04-17T18:30:00.000Z",
            "close": 92,
            "ews" : 2
    }, {
        "date": "2012-04-16T18:30:00.000Z",
            "close": 96,
            "ews" : 2 
    }, {
        "date": "2012-04-15T18:30:00.000Z",
            "close": 58,
            "ews" : 3
    }, {
        "date": "2012-04-12T18:30:00.000Z",
            "close": 105,
            "ews" : 1
    }, {
        "date": "2012-04-11T18:30:00.000Z",
            "close": 238,
            "ews" : 3
    }, {
        "date": "2012-03-29T18:30:00.000Z",
            "close": 108,
            "ews" : 1
    }, {
        "date": "2012-03-28T18:30:00.000Z",
            "close": 96,
            "ews" : 2
    }, {
        "date": "2012-03-27T18:30:00.000Z",
            "close": 198,
            "ews" : 0
    }];

}]);
app.directive('linearChart', function () {
    return {
        restrict: 'EA',

        link: function (scope, elem, attrs) {
            var data = scope.data;
            // Set the dimensions of the canvas / graph
            var margin = {
                top: 30,
                right: 20,
                bottom: 30,
                left: 50
            },
            width = 600 - margin.left - margin.right,
                height = 270 - margin.top - margin.bottom;

            // Parse the date / time
            var parseDate = d3.time.format("%d-%b-%y").parse;

            // Set the ranges
            var x = d3.time.scale().range([0, width]);
            var y = d3.scale.linear().range([height, 0]);

            // Define the axes
            var xAxis = d3.svg.axis().scale(x)
                .orient("bottom").ticks(5);

            var yAxis = d3.svg.axis().scale(y)
                .orient("left").ticks(5);

            // Define the line
            var valueline = d3.svg.line()
                .x(function (d) {
                return x(new Date(d.date));
            })
                .y(function (d) {
                return y(d.close);
            });

            // Adds the svg canvas
            var svg = d3.select("svg")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
                .append("g")
                .attr("transform",
                "translate(" + margin.left + "," + margin.top + ")");
            // Scale the range of the data
            x.domain(d3.extent(data, function (d) {
                return new Date(d.date);
            }));
            y.domain([0, d3.max(data, function (d) {
                return d.close;
            })]);

            // Add the valueline path.
            svg.append("path")
                .attr("class", "line")
                .attr("d", valueline(data));

            // Add the scatterplot
            svg.selectAll("dot")
                .data(data)
                .enter().append("circle")
                .attr("r", 3.5)
                .style("fill", function (d) {
                if (d.ews==0) return "green";
                if (d.ews==1) return "yellow";
                if (d.ews==2) return "orange";
                if (d.ews==3) return "red";
            })
                .attr("cx", function (d) {
                return x(new Date(d.date));
            })
                .attr("cy", function (d) {
                return y(d.close);
            });
                

            // Add the X Axis
            svg.append("g")
                .attr("class", "x axis")
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis);

            // Add the Y Axis
            svg.append("g")
                .attr("class", "y axis")
                .call(yAxis);

        }
    };
});
h1 {
    font-family: sans-serif;
    font-weight: bold;
    font-size: 16px;
}
body {
    font: 12px Arial;
}
path {
    stroke: #35cc99;
    stroke-width: 2;
    fill: none;
}
.axis path, .axis line {
    fill: none;
    stroke: gray;
    stroke-width: 1;
    shape-rendering: crispEdges;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Line chart</title>

    <link href="style.css" rel="stylesheet">

	<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
   <script src="controller.js"></script>
  </head>
  <body ng-controller="myController">
     <h1>My Chart</h1>
    <svg linear-chart></svg>
</body>
</html>

【问题讨论】:

    标签: angularjs d3.js svg tooltip linechart


    【解决方案1】:

    对于x轴/y轴上的标签做:

        //text label for y axis
        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("Value");
        // text label for the x axis
        svg.append("text")      
                .attr("x", width/2 )
                .attr("y", height +35)
                .style("text-anchor", "middle")
                .text("Date"); 
    

    我添加了一个 div 用于显示如下所示的工具提示

    <div id="mytooltip" style="position: absolute; z-index: 10; visibility: hidden; top: 82px; left: 81px;">
        <div id="ttclose"></div>
        <div id="ttdate"></div>
    </div>
    

    然后在移动事件中,您可以显示和设置值如下:

    .on("mouseover", function () {
                    return d3.select("#mytooltip").style("visibility", "visible"); //making the tooltip visible
                })
                    .on("mousemove", function (d) {
                    console.log()
                    d3.select("#mytooltip").style("top", (d3.mouse(this)[1] + 10) + "px").style("left", (d3.mouse(this)[0] + 10) + "px");
                    d3.select("#mytooltip").select("#ttdate").text(function () {
                        return d.date; //setting the date values to tooltip
                    });
                    d3.select("#mytooltip").select("#ttclose").text(function () {
                        return d.close; //setting the date values to tooltip
                    });
                    return;
                })
                    .on("mouseout", function () {
                    return d3.select("#mytooltip").style("visibility", "hidden"); //hidding the tooltip
                });
    

    工作代码here

    希望这会有所帮助!

    【讨论】:

    • 我希望日期文本稍微向下 +10 像素并加粗。 X 轴值为 11 月 2 日、11 月 3 日等日期。工具提示应显示值和时间。请解释您如何解析日期。请帮忙。我试过了,但整个对齐方式正在改变:(
    • 我得到了日期格式,例如 11 月 2 日,11 月 3 日,代码--> var xAxis = d3.svg.axis().scale(x) .orient("bottom").ticks(5 ).tickFormat(d3.time.format("%d %b"));
    • 为 x 轴的标签添加样式添加一个类 .attr("class", "xlabel") 为获得填充 @ teh 底部增加边距 底部:50, 对于日期格式,您做得正确。工作代码在这里jsfiddle.net/cyril123/kyjdu7ra/9
    • 对我来说,margin bottom:50 在日期文本下方创建一个填充。 svg 被视为包含文本和图形的整个元素,并且边距底部添加到整个 svg 的底部。我想在日期文本上方填充
    • @Wini 你可以添加标签向下移动的高度 svg.append("text") .attr("x", width/2 ) .attr("y", height +70) jsfiddle.net/cyril123/kyjdu7ra/12
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多