折线图

折线图

  const dataSet = [{
        country: 'China',
        group: [20, 40, 80, 30, 50, 60, 70, 20, 40, 80, 30, 50, 60, 70, 30]
    }, {
        country: 'English',
        group: [60, 50, 60, 70, 20, 40, 80, 30, 50, 40, 120, 30]
    }, {
        country: 'Japan',
        group: [60, 60, 70, 120, 40, 80, 30, 50, 60, 72, 40, 20]
    }];
    const width = 700, height = 500, padding = {top: 60, left: 30};
    const svg = d3.select("body").append("svg").attr("width", width + padding.left * 2).attr("height", height + padding.top * 2);
    const xScale = d3.scale.ordinal().domain(d3.range(R.pipe(R.sort(function (pre, cur) {
        return cur.group.length - pre.group.length;
    }), R.head, R.prop("group"), R.length)(dataSet))).rangeRoundBands([0, width], .3);
    var getLinearScale = R.pipe(R.sort(function (pre, cur) {
        return d3.max(cur.group) - d3.max(pre.group);
    }), R.head, R.prop("group"), d3.max);
    const yScale = d3.scale.linear().domain([0, getLinearScale(dataSet)]).range([height, 0]);
    const xAxis = d3.svg.axis().scale(xScale).orient("bottom").ticks(8);
    const yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(8);
    const color = d3.scale.category10();
    svg.append("g").attr("class", "axis").attr("transform", "translate(" + padding.left + "," + (height + padding.top) + ")").call(xAxis);
    svg.append("g").attr("class", "axis").attr("transform", "translate(" + padding.left + "," + padding.top + ")").call(yAxis);
    const linePath = d3.svg.line().interpolate("basis").x(function (d, i) {
        console.log(xScale(i) + padding.left + xScale.rangeBand() / 2);
        return xScale(i) + padding.left + xScale.rangeBand() / 2;
    }).y(function (d) {
        return yScale(d) + padding.top;
    });
    svg.append("g").attr("class", "line").selectAll("path").data(dataSet).enter().append("path").attr("d", function (d) {
        return linePath(d.group);
    }).attr("fill", "none").attr("stroke", function (d, i) {
        return color(i);
    }).attr("stroke-width", 2);
    //    此处为了练习符号生成器使用了symbol以及定义了它的type,size访问器 **实际表图中建议使用rect**
    const symbolPath = d3.svg.symbol()
        .type(function (d) {
            return d3.svg.symbolTypes[d];
        }).size(180);
    svg.append("g").attr("class", "symbol").selectAll("path").data(dataSet).enter().append("path").attr("d", function (d, i) {
        return symbolPath(i)
//        因为path是路径元素 没有高宽 没法用x y来定位 只能用transform来移动
    }).attr("transform", function (d, i) {
        return "translate(" + ((i * 100) + padding.left + 30) + "," + (height + padding.top * 1.6) + ")";
    }).attr("fill", function (d, i) {
        return color(i);
    });
    svg.append("g").attr("class", "text").selectAll("text").data(dataSet).enter().append("text").text(function (d, i) {
        return d.country;
    }).attr("transform", function (d, i) {
        return "translate(" + ((i * 100) + padding.left + 70) + "," + (height + padding.top * 1.6) + ")";
    }).attr("dy", 5).attr("fill", function (d, i) {
        return color(i);
    }).attr("text-anchor", "middle");

结果:

折线图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 折线图是工作中使用最频繁的图表之一,大多数人对折线图的认识还是非常浅显的,接下来,来深入了解一下折线图。 一.什么...
    璞石阅读 14,831评论 0 14
  • 首先先看一下效果: 一、 初始化折线图对象 创建一个折线图的用到的类是LineChartView.h, 代码如下:...
    jianshu_wl阅读 20,627评论 50 151
  • 说明: 已将折线图封装到了一个UIView子类中,并提供了相应的接口。若你遇到相应的需求可以直接将文件拖到项目中,...
    AHLQ阅读 12,157评论 5 47
  • 折线图是通过在它们之间绘制线段来连接一系列点的图。 这些点在它们的坐标(通常是x坐标)值之一中排序。 折线图通常用...
    yuanyb阅读 11,585评论 0 1
  • 基于HTTP协议的轻量级开源简单队列服务:HTTPSQS(引用地址:http://zyan.cc/httpsqs/...
    Fairyin阅读 6,940评论 0 4