boosalis's avatar
boosalis
Frequent Visitor
3 years ago

Motion Chart and 3D Cube in PowerBI

Select between the visuals using the select boxes at the top of the screen. 

 

I always wanted to be able to have a decent motion chart and draggable 3D cube in Power BI.  

 

Select between the visuals using the select boxes at the top of the screen. 

 

I tried to use code from the packages that render these visualizations using MS R and Power BI, but my efforts were unsuccessful.

What did work was using the HTML visualizer from the PBI Marketplace.  I exported the HTML from the R Studio for the motion cart and the cube, and then dropped the files into webspace.  Once I added the URLs to the visualizer in PBI and published it to the web, I finally got what I wanted.

 

Yeah, it’s quite a bit of a hack, but I’m happy anyway.

 

Note that you can rotate the cube, select items on the legend, and push the play button on the motion chart. 

 

3 Replies

  • boosalis's avatar
    boosalis
    Frequent Visitor

    odeddror 

     

    Suely! I cant seem to add the .r file, so I will just post it here. After running the code in R studio, exporting the plots in HTML, and uploading the files to a server, I dropped the URLs in the sample .pbi file that comes with the HTML 5 widget from the market place. Hope that it helps others. 

     

    3D Cube in R

     

    ####################################################
    #I loaded each section by hand to avoid errors in R Studio
    
    library(plotly)
    library(htmltools)
    library(htmlwidgets)
    library(rmarkdown)
    library(xfun)
    library(iris)
    library(stringr)
    library(datasets)
    data(iris)
    
    #This is the famous iris dataset.
    rep_str = c('setosa'='1','versicolor'='2','virginica'='3')
    iris$Species <- str_replace_all(iris$Species, rep_str)
    iris$Species
    
    #I changed the species to suit my demo. 
    iris$Species <- as.numeric(as.character(iris$Species))
    iris$Species[which(iris$Species == 1)] <- '1. Initial Observation';
    iris$Species[which(iris$Species == 2)] <- '2. Midterm Observation';
    iris$Species[which(iris$Species == 3)] <- '3. Final Observation';
    iris$Species <- as.factor(iris$Species)
    
    #Color the plot points. 
    fig <- plot_ly(iris, type= "scatter3d", mode = "markers",
                    x = ~Sepal.Length, 
                    y = ~Sepal.Width, 
                    z = ~Petal.Length, 
                    color = ~iris$Species,
                    colors = c('#BF382A', '#0C4B8E', '#0bfc03'))
                   
    #Legend Labels
    g <- fig %>% add_markers()
    g <- fig %>% layout(scene = 
      list(xaxis = list(title = 'Supervisor Evaluation'),
      yaxis = list(title = 'Student Self-Evaluation'),
      zaxis = list(title = 'Months in Program')))
    
    #Generate the plot
    g
    
    ####################################################
    #The code was cobbled together from various help forums; I merely cobbled. 
    #I saved the plot as a webpage and dropped the files into a web host.
    #PowerbI needs the URL to the html file to load the plot in the HTML 5 Widget.
    #Repeat for Motion Chart

     

     

    GapMinder Motion Chart

     

    #This is all from plotly. 
    
    library(plotly)
    library(gapminder)
    
    df <- gapminder 
    fig <- df %>%
      plot_ly(
        x = ~gdpPercap, 
        y = ~lifeExp, 
        size = ~pop, 
        color = ~continent, 
        frame = ~year, 
        text = ~country, 
        hoverinfo = "text",
        type = 'scatter',
        mode = 'markers'
      )
    fig <- fig %>% layout(
      xaxis = list(
        type = "log"
      )
    )
    
    fig

     

     

    I used the HTML Content thingy from the market place to create the demo. The sample file is there, and I just modified the links and the look in PBI.

     

     

    Regards,

    Chris B.