jjzjj

javascript - NVD3 多轴条形图在绘图上重叠条形图

coder 2024-07-16 原文

使用 nvd3 绘制多轴条形图。我的问题是条形重叠。在图表中,y 轴在左侧,另一个 y 轴在右侧。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.3/nv.d3.css" rel="stylesheet" type="text/css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.3/nv.d3.js"></script>
    <style>
        text {
            font: 12px sans-serif;
        }
        svg {
            display: block;
        }
        html, body, #chart1, svg {
            margin: 0px;
            padding: 0px;
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body class='with-3d-shadow with-transitions'>

<div id="chart1" >
    <svg> </svg>
</div>

<script>

    // var testdata = stream_layers(9,10+Math.random()*100,.1).map(function(data, i) {
    //     return {
    //         key: 'Stream' + i,
    //         values: data.map(function(a){a.y = a.y * (i <= 100 ? 100 : 1); return a})
    //     };
    // });
     var testdata=[{"key":"stream1", "values": [
    {
      "x": 0,
      "y": 44
    },
    {
      "x": 1,
      "y": 10
    },
    {
      "x": 2,
      "y": 29
    },
    {
      "x": 3,
      "y": 88
    },
    {
      "x": 4,
      "y": 25
    },
    {
      "x": 5,
      "y": 32
    },
    {
      "x": 6,
      "y": 100
    },
    {
      "x": 7,
      "y": 52
    },
    {
      "x": 8,
      "y": 15
    },
    {
      "x": 9,
      "y": 78
    },
    {
      "x": 10,
      "y": 42
    },
    {
      "x": 11,
      "y": 108
    },
    {
      "x": 12,
      "y": 17
    },
    {
      "x": 13,
      "y": 23
    },
    {
      "x": 14,
      "y": 182
    },
    {
      "x": 15,
      "y": 9
    },
    {
      "x": 16,
      "y": 25
    },
    {
      "x": 17,
      "y": 90
    },
    {
      "x": 18,
      "y": 32
    },
    {
      "x": 19,
      "y": 138
    },
    {
      "x": 20,
      "y": 189
    },
    {
      "x": 21,
      "y": 3
    },
    {
      "x": 22,
      "y": 16
    },
    {
      "x": 23,
      "y": 66
    },
    {
      "x": 24,
      "y": 46
    },
    {
      "x": 25,
      "y": 27
    },
    {
      "x": 26,
      "y": 185
    },
    {
      "x": 27,
      "y": 13
    },
    {
      "x": 28,
      "y": 12
    },
    {
      "x": 29,
      "y": 71
    },
    {
      "x": 30,
      "y": 191
    }
  ]},
  {"key":"stream2","values":[
    {
      "x": 0,
      "y": 1.1
    },
    {
      "x": 1,
      "y": 0.5
    },
    {
      "x": 2,
      "y": 2.1
    },
    {
      "x": 3,
      "y": 1.5
    },
    {
      "x": 4,
      "y": 1.7
    },
    {
      "x": 5,
      "y": 2.1
    },
    {
      "x": 6,
      "y": 0.75
    },
    {
      "x": 7,
      "y": 1.75
    },
    {
      "x": 8,
      "y": 1
    },
    {
      "x": 9,
      "y": 2.3
    },
    {
      "x": 10,
      "y": 2
    },
    {
      "x": 11,
      "y": 0.5
    },
    {
      "x": 12,
      "y": 1.6
    },
    {
      "x": 13,
      "y": 1.8
    },
    {
      "x": 14,
      "y": 2.35
    },
    {
      "x": 15,
      "y": 2.4
    },
    {
      "x": 16,
      "y": 1.8
    },
    {
      "x": 17,
      "y": 1
    },
    {
      "x": 18,
      "y": 1.25
    },
    {
      "x": 19,
      "y": 1.85
    },
    {
      "x": 20,
      "y": 0.65
    },
    {
      "x": 21,
      "y": 0.75
    },
    {
      "x": 22,
      "y": 1.25
    },
    {
      "x": 23,
      "y": 2.25
    },
    {
      "x": 24,
      "y": 0.5
    },
    {
      "x": 25,
      "y": 1.85
    },
    {
      "x": 26,
      "y": 1.75
    },
    {
      "x": 27,
      "y": 1.15
    },
    {
      "x": 28,
      "y": 1.9
    },
    {
      "x": 29,
      "y": 2.4
    },
    {
      "x": 30,
      "y": 1.5
    }
  ]}];
    testdata[0].type = "bar";
    testdata[0].yAxis = 1;
    testdata[1].type = "bar";
    testdata[1].yAxis = 2;
    console.log(testdata);

    nv.addGraph(function() {
        var chart = nv.models.multiChart()
            .margin({top: 30, right: 60, bottom: 50, left: 70})
            .color(d3.scale.category10().range())
            .height(450)
            .width(1200)
            .color(d3.scale.category10().range())
            .useInteractiveGuideline(true)
            .interpolate('linear');

        chart.xAxis.tickFormat(d3.format(',f'));
        chart.yAxis1.tickFormat(d3.format(',.1f'));
        chart.yAxis2.tickFormat(d3.format(',.1f'));

        d3.select('#chart1 svg')
            .datum(testdata)
            .transition().duration(500).call(chart);

        return chart;
    });

</script>
</body>
</html>

Plunkr 设置 embeded为此可以找到here .

两组数据绘制多图 但是条是重叠的。我的输出屏幕截图在这里。

唯一要做的就是调整条的宽度和位置。那些可以由类(class)选择

d3.selectAll ('.bars1Wrap .nv-groups .nv-series-0 rect') 

当我将轴切换到

testdata[1].yAxis = 1;

它工作完美。

但问题是丢失的数据范围。我需要两个数据集的不同范围。

我尝试在绘制图表后调整条形的宽度

喜欢

var g3 =  d3.selectAll ('.bars1Wrap .nv-groups .nv-series-0 rect') 
                             .attr("width", function(d){ return d/2;});

但是没有成功... 任何帮助表示赞赏。

最佳答案

当然,nvd3 在 multiChart 上有问题,当您为需要显示的两个可视化数据集选择类型 “bar” 时。

我要说的是不是这个

testdata[0].type = "bar";
testdata[0].yAxis = 1;
testdata[1].type = "bar";
testdata[1].yAxis = 2;

如果是

testdata[0].type = "line";
testdata[0].yAxis = 1;
testdata[1].type = "bar";
testdata[1].yAxis = 2;

它工作正常。

因此,为了让两个条形图不堆叠。 需要进行一些开箱即用的调整,在渲染图表后调用此函数。

function resetBarSize(d1){
      //get the width of the bar, and make it half
      var w2 = d3.select(".bars2Wrap .nv-bar").attr("width")/2;
      if (!d1){
        //initial load d1 will be undefined
        //in that case make all the bars half
        d3.selectAll(".bars1Wrap .nv-bar").style("width", w2);
        d3.selectAll(".bars2Wrap .nv-bar").style("width", w2);
        //translate the last bar so that there is no overlapping
        d3.selectAll(".bars2Wrap .nv-bar")[0].forEach(function(d){
          var t = d3.transform(d3.select(d).attr("transform")),
          x = t.translate[0] + w2,
          y = t.translate[1];
          d3.select(d).attr("transform", "translate(" + x +"," + y + ")");
        })        
      }else if (d1.yAxis ==2 && d1.disabled){
        //in this case axis 2 is disabled or not visible so make bar1 width double.
        d3.selectAll(".bars1Wrap .nv-bar").style("width", w2 *2);
      }else if (d1.yAxis ==1 && d1.disabled){
        //in this case axis 1 is disabled or not visible so make bar1 width double.
        d3.selectAll(".bars2Wrap .nv-bar").style("width", w2 *2);
      } else {
        //in this case axis both axis is present. Make all the bars half and translate bar 2 so that they don't overlap.

        d3.selectAll(".bars1Wrap .nv-bar").style("width", w2);
        d3.selectAll(".bars2Wrap .nv-bar").style("width", w2);
        d3.selectAll(".bars2Wrap .nv-bar")[0].forEach(function(d){
          var t = d3.transform(d3.select(d).attr("transform")),
          x = t.translate[0] + w2,
          y = t.translate[1];
          d3.select(d).attr("transform", "translate(" + x +"," + y + ")");
        })   
      }
      return;
    }

现在在渲染图表后调用此函数。

nv.dispatch.on('render_end', function(newState) {
  resetBarSize();
  chart.legend.dispatch.on('legendClick', function(newState) {
      chart.update();
      setTimeout(function(){resetBarSize(newState)});
});

工作代码 here

关于javascript - NVD3 多轴条形图在绘图上重叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36951247/

有关javascript - NVD3 多轴条形图在绘图上重叠条形图的更多相关文章

  1. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  2. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  3. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

  4. ruby - 在 StockChart (highchart) 中以编程方式显示柱形图的工具提示 - 2

    我有一个Highstock图表(带有标记和阴影的线条),并且想以编程方式显示一个highstock工具提示,例如,当我选择某个表上的一行(包含图表数据)我想显示相应的highstock工具提示。这可能吗? 最佳答案 股票图表thissolution不起作用:在thisexample你必须更换这个:chart.tooltip.refresh(chart.series[0].data[i]);为此:chart.tooltip.refresh([chart.series[0].points[i]]);解决方案可用here.

  5. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  6. ruby-on-rails - 我将 Rails3 与 tinymce 一起使用。如何呈现用户关闭浏览器javascript然后输入xss? - 2

    我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如

  7. ruby - 使用 Selenium WebDriver 启用/禁用 javascript - 2

    出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=

  8. ruby - Watir-Webdriver 是否支持点击目标为 javascript 的链接? - 2

    我是Ruby和Watir-Webdriver的新手。我有一套用VBScript编写的站点自动化程序,我想将其转换为Ruby/Watir,因为我现在必须支持Firefox。我发现我真的很喜欢Ruby,而且我正在研究Watir,但我已经花了一周时间试图让Webdriver显示我的登录屏幕。该站点以带有“我同意”区域的“警告屏幕”开头。用户点击我同意并显示登录屏幕。我需要单击该区域以显示登录屏幕(这是同一页面,实际上是一个表单,只是隐藏了)。我整天都在用VBScript这样做:objExplorer.Document.GetElementsByTagName("area")(0).click

  9. 网页设计期末作业,基于HTML+CSS+JavaScript超酷超炫的汽车类企业网站(6页) - 2

    🎉精彩专栏推荐💭文末获取联系✍️作者简介:一个热爱把逻辑思维转变为代码的技术博主💂作者主页:【主页——🚀获取更多优质源码】🎓web前端期末大作业:【📚毕设项目精品实战案例(1000套)】🧡程序员有趣的告白方式:【💌HTML七夕情人节表白网页制作(110套)】🌎超炫酷的Echarts大屏可视化源码:【🔰Echarts大屏展示大数据平台可视化(150套)】🔖HTML+CSS+JS实例代码:【🗂️5000套HTML+CSS+JS实例代码(炫酷代码)继续更新中…】🎁免费且实用的WEB前端学习指南:【📂web前端零基础到高级学习视频教程120G干货分享】🥇关于作者:💬历任研发工程师,技术组长,教学总监;

  10. ruby-on-rails - 在页面的最底部包含 javascript 文件 - 2

    我有一个Rails应用程序。还有一个javascript(javascript1.js)文件必须包含在每个View的最底部。我把它放在/assets/javascripts文件夹中。Application.js包含以下代码//=requirejquery//=requirejquery_ujs//=someotherfiles//=require_directory.即使Application.js中不包含javascript1.js,它也会自动包含,不是吗?那么我怎样才能做我想做的事呢? 最佳答案 单独定义、包含和执行您的java

随机推荐