简体   繁体   English

隐藏 dc.js 图表 x 轴

[英]Hide dc.js chart x-axis

As the image below, the x-axis is very messy due to big range of data.如下图,由于数据范围大,x 轴非常混乱。 I wish to remove the x-axis, any luck?我想删除 x 轴,任何运气?

隐藏 dc.js 图表 x 轴

my current code:我目前的代码:

toneChart.width(300).height(280)
    .dimension(tone)
    .group(toneGroup)
    .title(function (d) { return ""; })
    .ordering(function(d) { return - d.value })
    .cap(10)
    .transitionDuration(750)
    .renderLabel(true)
    .colors(d3.scale.category10())
    .elasticX(true);

Thanks!谢谢!

Via CSS one can hide the axes and text.通过 CSS 可以隐藏轴和文本。

Remove row chart's x-axis删除行图的 x 轴

Add the following to your CSS (replacing the #ID with yours):将以下内容添加到您的 CSS(用您的 #ID 替换):

#your-row-chart svg g g.axis.x { display: none; }

Remove bar chart's y-axis删除条形图的 y 轴

Add the following to your CSS (replacing the #ID with yours):将以下内容添加到您的 CSS(用您的 #ID 替换):

#your-bar-chart svg g g.axis.y { display: none; }

You can control the formatting of the values on the X-axis through the .xAxis().tickFormat() method, which comes from D3.您可以通过来自 D3 的 .xAxis().tickFormat() 方法控制 X 轴上值的格式。

// To format as a percent
chart.xAxis().tickFormat(function(v) { return 100 * v/yourDenominator + "%"; });

// To blank the the values entirely, though not the actual ticks marks themselves 
chart.xAxis().tickFormat(function(v) { return ""; });

Here's a link to the documentation这是文档的链接

https://github.com/dc-js/dc.js/blob/master/web/docs/api-1.6.0.md#xaxisxaxis https://github.com/dc-js/dc.js/blob/master/web/docs/api-1.6.0.md#xaxisxaxis

I've found it best to do this kind of thing outside of the traditional stack of configuration methods because if you include .xAxis().tickFormat() in with everything else then the next method in the stack will, of course, be associated with the D3 object emitted by the .tickFormat() call, which can result in maddening errors.我发现最好在传统的配置方法堆栈之外执行此类操作,因为如果您将 .xAxis().tickFormat() 与其他所有内容一起包含在内,那么堆栈中的下一个方法当然会相关联使用 .tickFormat() 调用发出的 D3 对象,这可能会导致令人抓狂的错误。

Funny!有趣的! I just solved it!我刚刚解决了!

Tips: adjust the chart margins.提示:调整图表边距。 Dirty but working fine for me :-P脏但对我来说很好用 :-P

.margins({top: 0, right: 0, bottom: -1, left: 0})

在此处输入图片说明

I had the same question for y-axis, and here's one way to do it in the library itself:我对 y 轴有同样的问题,这是在图书馆本身中做到这一点的一种方法:

  1. After the line var _yElasticity = false;var _yElasticity = false;行之后var _yElasticity = false; add:添加:

     var _yVisibility = true;
  2. After the declaration of _chart.elasticY = function (_) { ... } define a new function .showYAxis([boolean]) by adding:_chart.elasticY = function (_) { ... }声明之后,通过添加以下内容定义一个新函数.showYAxis([boolean])

     /** #### .showYAxis([boolean]) Turn on/off y axis. **/ _chart.showYAxis = function (_) { if (!arguments.length) { return _yVisibility; } _yVisibility = _; return _chart; };
  3. Change the condition that determines when the y-axis is rendered from更改确定何时呈现 y 轴的条件

     if (_chart.elasticY() || render) { _chart.renderYAxis(_chart.g()); }

to

        if ((_chart.elasticY() || render) && (_chart.showYAxis()) ) {
            _chart.renderYAxis(_chart.g());
        }
  1. Now you can remove the y-axis in your charts by simply adding .showYAxis(false) .现在,您只需添加.showYAxis(false)即可删除图表中的 y 轴。

You can try你可以试试

toneChart.xAxis().ticks(0)

and in your CSS (erase the axis)并在您的 CSS 中(擦除轴)

.toneChart .axis path, .axis line{
stroke: none;
}

Based on @VictorCortes answer, this strikes me as the simplest way to do it.根据@VictorCortes 的回答,我认为这是最简单的方法。 No CSS, no modification of the library.没有 CSS,没有修改库。

Using xAxis or yAxis depending on your chart type :根据您的图表类型使用xAxisyAxis

toneChart.yAxis().tickFormat(function(v) {return "";});
toneChart.yAxis().ticks(0);

This is a working TypeScript solution.It searches for 'axis x' class list and hide the xAxis element.这是一个有效的 TypeScript 解决方案。它搜索“axis x”类列表并隐藏 xAxis 元素。

public checkXAxisVisibility() {
        const elements = document.getElementsByClassName('axis x');
        if (elements && elements.length > 0) {
            const xAxis = elements[0] as HTMLElement;
            xAxis.style.display = this.chartHasData ? 'block ' : 'none';
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM