Highcharts/HighcharteR - draw a polygon with rounded corners

241 views Asked by At
library(highcharter)
highchart() %>% 
  hc_add_series(name='Polygon',type='polygon',data=list(c(1,4),c(2,4), c(3,3), c(2,3)), 
                borderRadius = 10, lineColor = "red", lineWidth = 3)][1]][1]

enter image description here

Hello everybody. I use a polygon to display some data. I would prefer to have the borders to be round, but the borderRadius attribute does not work for the polygon.

Does anyone have an idea how to archieve a rounded look of my polygon? Documentation did not help in this case :-(. This is made the the R Highcharter package, but I would also be totally fine with an example in die native JS Library. Thank you!

1

There are 1 answers

0
Data Mastery On

This works somewhat:

spline.poly <- function(xy, vertices, k=3, ...) {
  # Assert: xy is an n by 2 matrix with n >= k.
  
  # Wrap k vertices around each end.
  n <- dim(xy)[1]
  if (k >= 1) {
    data <- rbind(xy[(n-k+1):n,], xy, xy[1:k, ])
  } else {
    data <- xy
  }
  
  # Spline the x and y coordinates.
  data.spline <- spline(1:(n+2*k), data[,1], n=vertices, ...)
  x <- data.spline$x
  x1 <- data.spline$y
  x2 <- spline(1:(n+2*k), data[,2], n=vertices, ...)$y
  
  # Retain only the middle part.
  cbind(x1, x2)[k < x & x <= n+k, ]
}


  X <- matrix(c(resultdf$yAxis, resultdf$xAxis), ncol=2)
  hpts <- chull(X) # Creates indices of a convex hull from a matrix
  hpts <- c(hpts, hpts[1]) # connect last and first dot
  hpts <- data.frame(X[hpts, ])
  hpts <- data.frame(spline.poly(as.matrix(data.frame(hpts$X1, hpts$X2)),  500)) %>%
    setNames(c("yAxis", "xAxis"))

the spline.poly function creates a lot of new points which connect to a more rounded shape :-)