rCharts nvd3库强制滴答 [英] rCharts nvd3 library force ticks

查看:167
本文介绍了rCharts nvd3库强制滴答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制所有刻度线和刻度标签沿 rCharts nPlot从 nvd3 library。我尝试了几种方法,但没有成功。

I would like to force all tick marks and tick labels to appear along the axis in an rCharts nPlot from the nvd3 library. I have tried several approaches without success.

这是默认行为:

df <- data.frame(x = 1:13, y = rnorm(13))
library(rCharts)
n <- nPlot(data = df, y ~ x, type = 'lineChart')
n$yAxis(showMaxMin = FALSE)

我想要所有数据在<$

I would like to have all data in 1:13 show along the x axis.

最后,我有自定义的标记我想显示

Ultimately, I have custom tickmarks I want to show equally-spaced with the following replacement:

n$xAxis(tickFormat = "#! function (x) { 
    ticklabels = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', 
        '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', 
        '2030-2050', '2050-2070', '2070-2100']
        return ticklabels[x-1];
} !#")

我希望很清楚为什么我想要所有的刻度线和

To give an idea of the finished product, here is a ggplot2 impression:

要想了解成品,请点击 ggplot2

library(ggplot2) df <- data.frame(x = c('0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050', '2050-2070', '2070-2100'), y = rnorm(13), z = "group1") ggplot(data = df, aes(x = x, y = y, group = z)) + geom_line()

>

几个我试过的东西,基于我在这里和那里找到的几个建议:没有工作。

Based on my reading of the docs, I thought this would work:

根据我对文档的阅读,我认为这会工作:

n$xAxis(tickFormat = "#! function (x) { return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13][x-1]; } !#")

I also tried this, on the off chance:

off chance:

n$xAxis(ticks = 13)

I also tried to combine tickValues and tickFormat but with no success.

我也尝试结合 tickValues tickFormat 没有成功。

I also thought about writing a script, but again my understanding of the nvd3 library was insufficient.

我也想过写一个脚本,但是我对 nvd3 库的理解还不够。

n$setTemplate(afterScript = "<script> var chart = nv.models.lineChart(); var newAxisScale = d3.scale.linear(); .range(d3.extent(chart.axes[0]._scale.range())) .domain([1, d3.max(chart.axes[0]._scale.domain())]) chart.axes[0].shapes.call( d3.svg.axis() .orient('bottom') .scale(newAxisScale) .ticks(13) //.tickValues() //.tickFormat(d3.format()) ).selectAll('text') .attr('transform','') </script>" )

None of these report errors in the console, but none of them modify the appearance of the first plot above.

这些都没有在控制台中报告错误,但是没有一个会修改上面第一个图的外观。

解决方案

code> tickFormat 。这是一个 rCharts 解决方案。相应的 d3 nvd3 解决方案应该很容易推导出来。

It turns out I was not correctly setting tickValues as I got the syntax confused with tickFormat. Here is an rCharts solution. The corresponding d3 or nvd3 solution should be easy to deduce.

n <- nPlot(data = df, y ~ x, type = 'lineChart')
n$yAxis(showMaxMin = FALSE)
n$addParams(height = 500, width = 1000)
n$xAxis(tickValues = "#! function (x) {    
    tickvalues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
    return tickvalues;
} !#")
n$xAxis(tickFormat = "#! function (x) {
    tickformat = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', 
       '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050', 
       '2050-2070', '2070-2100'];
    return tickformat[x-1];
} !#")
n

注意代码 tickvalues in tickValues but tickformat [x-1] c $ c> tickFormat 。

Notice how the code has tickvalues in tickValues but tickformat[x-1] in tickFormat.

这篇关于rCharts nvd3库强制滴答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆