How to change the axis title with rCharts, dPlot and dimple

守給你的承諾、 提交于 2019-12-06 06:04:17

Sorry I just saw this. Thanks John for answering.

With rCharts, we can take advantage of the afterScript template to add this. If there is only one chart in the DOM, we can use John's example unmodified.

d1$setTemplate(
  afterScript = 
  '
    d3.selectAll(".axis.title")
    .text(function () {
      var t = d3.select(this).text();
      if (t === "disp") {
          return "Displacement";
      } else if (t === "mpg") {
          return "Miles Per Gallon";
      } else {
          return t;
      }
    }); 
  '
)

Please let me know if this you would like an example with multiple charts in the DOM or this does not work for you. Thanks.

Dimple doesn't currently expose the titles, however it's coming in the next release. Once it does I'm sure the great guys behind the dimple implementation in rcharts will add them into the library. I'm not quite sure how this works with an R implementation but if you can run some Javascript once the chart is rendered you can modify the titles using some raw d3:

d3.selectAll(".axis.title")
    .text(function () {
        var t = d3.select(this).text();
        return (t === "disp" ? "Displacement" : t);
    }); 

If you want to extend this to replace a couple of titles you can do it with:

d3.selectAll(".axis.title")
    .text(function () {
        var t = d3.select(this).text();
        if (t === "disp") {
            return "Displacement";
        } else if (t === "mpg") {
            return "Miles Per Gallon";
        } else {
            return t;
        }
    }); 

I hope this helps.

PatrickT

Here is another way:

# devtools::install_github("rCharts", "ramnathv", ref = "dev")
library(rCharts)
data(mtcars)
mtcars.df <- data.frame( car = rownames(mtcars), mtcars )
d1 <- dPlot(x ="disp", y="mpg", groups=c("car", "cyl"), type ="point", data=mtcars.df)
d1$xAxis( type = "addMeasureAxis")
d1

d1$setTemplate(afterScript = "
  <script>
    myChart.draw()
    myChart.axes[0].titleShape.text('Displacement')
    myChart.axes[1].titleShape.text('Miles Per Gallon')
    myChart.svg.append('text')
        .attr('x', 40)
        .attr('y', 20)
        .text('Plot of Miles Per Gallon / Displacement')
        .style('text-anchor','beginning')
        .style('font-size', '100%')
        .style('font-family','sans-serif')
  </script>               
")
d1

Screenshot:

Hat tip to Ramnath: R: interactive plots (tooltips): rCharts dimple plot: formatting axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!