javascript - 如何隐藏 n3 图表中第一个子项和最后一个子项的刻度

标签 javascript d3.js linechart

我正在使用 n3-charts,它使用 line-chart.min.js 和 d3.v2.js 插件。我想隐藏 x 轴第 0 个刻度值和最后一个子刻度值。具体怎么做,如下图所示。 enter image description here

请有人帮我解决这个问题,我不希望 APR11 和最后一个刻度值动态变化。

app.directive('barChart', [
                       function () {

                           return {
                               restrict: 'EA',
                               template: '<linechart data="data" options="options" width="550" height="291"></linechart>',
                               scope: {
                                   range: '=',
                               },
                               link: function(scope, elements, attrs){
                                   scope.$watch("range", function () { 
                                   var values = scope.range;
                                   scope.data = values;
                                       scope.options = {
                                           yaxis  : {name: "Calories",labelalign:"-135"},
                                           stacks: [{axis: "y", series: ["firstVal", "secondVal", 'thirdVal']}],
                                           fonts:  {family: 'serif', size: '14px'},
                                           axes: {
                                               x: {key: 'x', type: 'date',labelFunction: function(x) { return d3.time.format('%b %d')(new Date(x));}},
                                               y: {key :'y',type: 'linear',min:0}
                                           },
                                           transition: {ease: 'elastic', duration: 1000, delay: 100},
                                           series: [
                                               {id: 'secondVal', y: 'secondVal', axis: 'y', color: "#b3d660", type: 'column', drawDots: true, dotSize: 4, label: 'Labe1'},
                                               {id: 'firstVal', y: 'firstVal', axis: 'y', color: "#ff8669", thickness: '2px', type: 'column', label: 'Label2'},
                                               {id: 'thirdVal', y: 'thirdVal', axis: 'y', color: "#52c4dc", type: 'column', dotSize: 2, label: 'Label3'}
                                           ],
                                           lineMode: 'linear',
                                           tension: 0.2,
                                           tooltip: {
                                               mode: 'scrubber', formatter: function (x, y, series) {
                                                   return series.label + ', ' + Math.round(y);
                                               }
                                           },
                                           drawLegend: true,
                                           drawDots: true,
                                           columnsHGap: 5
                                       }
                                   });
                               }

                           };}]);

最佳答案

对于n3-charts v2,您可以为每个轴定义一个tickFormat函数。

向scope.options.axes.x添加一个名为tickFormat的函数,其内容如下:

tickFormat: function(value, index){
    var firstOrLast = (index === 0) || (index === scope.values.length-1);
    return firstOrLast ? "" : value;
}

不确定 n3-charts v1,但我发现您在scope.options.axes.x 中已经有一个名为 labelFunction 的函数。您可以执行与上述代码类似的操作,但只需检查参数“x”是否与scope.values 中的第一个或最后一个值匹配。

关于javascript - 如何隐藏 n3 图表中第一个子项和最后一个子项的刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218176/

相关文章:

javascript - 在 Bootstrap 模式中错误地显示 d3.js

javafx-2 - JavaFX 2.x : XYChart properties

javascript - Uncaught ReferenceError : [functionName] is not defined

javascript - jQuery代码仅在第一个div点击时播放声音

javascript - 使用 d3-cloud 动态调整词云大小

javascript - 谷歌图表 : Switching between Line and Column charts

java - 如何更改 CSS 第 7 系列上方的 javaFX LineChart 中的图标?

javascript - 如何在模型和输入框之间有不同的值(Angular 5)

javascript - 重现气泡js和css

javascript - .exit().remove() 似乎删除了折线图图例中的文本,但没有删除矩形