Genie Discord forum

Author Avataritsdfish
3/27/2023, 10:18:01 PM

Hi all,

I have a few questions related to plotting. I would like to use Genie to create a dashboard with various plots. Where do I find information about plotting options, such as adding axis labels, turning of grid, setting x and y ranges, ticks etc? Based on a cursory look of the source code, it seems like those are not options that can be passed to the PlotData function.

I am fairly familiar with Plots.jl. Is there a way to use that instead?

Update: I found PlotLayoutAxis in an example in the app gallery, but it seems like an old version of Genie. Its not clear to me how to add axis labels.

Author Avataritsdfish
3/28/2023, 9:14:08 PM

Thanks for your help. I tried to add a layout for the axis labels but it doesn't work. Do you have any recommendations?


  using GenieFramework
  using Distributions

  @genietools 
    @handlers begin
      @in mu = 0.0
      @in sigma = 1.0
      @out pdf_plot = PlotData()

      layout = PlotLayout(
                      xaxis = [PlotLayoutAxis(title = "x")],
                      yaxis = [PlotLayoutAxis(xy = "y", title = "density")])

      @onchange isready, mu, sigma begin 
        dist = Normal(mu, sigma)
        q1,q99 = quantile(dist, [.001, .999])
        x = [range(-5, q1, length=5)..., 
            range(q1, q99, length=100)..., 
            range(q99, 5, length=5)...]
        y = pdf.(dist, x)
        pdf_plot = PlotData(;x, y, mode="lines", layout)
      end
    end

  @page("/", "app.jl.html")
  Server.up()
  wait(Condition())
end
Author Avatarabhimanyuaryan
3/30/2023, 4:12:35 AM

can you post the output screenshot?

Author Avatarabhimanyuaryan
3/30/2023, 4:39:48 AM

I think you need to mention

xy for first as well

[PlotLayoutAxis(xy= "x", title = "my-x-title")]
Author Avataritsdfish
3/30/2023, 9:16:25 AM

@abhimanyuaryan , thanks for your help. This raises a different but related point. Yesterday, I did not receive an error. Now that I have restarted the server (and implemented your suggested change), I receive an error indicating PlotData does not have a keyword layout. After removing layout I continue to receive the error until I restart the server. This slows down the development process dramatically. Is there somewhere I can file an issue?

I uploaded a screenshot in case it is still useful.

Where should I pass the layout argument? One example shows that it is passed to PlotData , but the source code does not show that layout is a keyword.

By the way, part of my goal here is to construct a MWE showing how to add axis titles, remove the grid lines, and specify limits on they x and y axis values. If this would be useful, I can add it somewhere to the documentation, or at least file an issue with the finished example.

Author Avatarabhimanyuaryan
3/31/2023, 2:14:24 AM

layout should passed separate in plot(data, layout, ..)

Author Avatarabhimanyuaryan
3/31/2023, 2:14:50 AM

can you upload your complete MWE(current) so I can just run and test it?

Author Avataritsdfish
3/31/2023, 6:59:54 AM

@abhimanyuaryan , thanks for taking a look. I tried plot(data, layout, but it lead to an method error with the closest candidate being plot(::Union{AbstractString, Symbol}, ::Any...; layout, config, configtype, syncevents, syncprefix, class, kwargs...).

I uploaded the code.

Author Avatarabhimanyuaryan
4/3/2023, 6:03:26 AM

this is not correct

pdf_plot = PlotData(x, y, layout, mode="lines")
Author Avatarabhimanyuaryan
4/3/2023, 6:04:30 AM

PlotData object doesn't take layout if you see the example here: https://github.com/GenieFramework/GenieFrameworkDemos_NewAPI/blob/main/BasicApplication/app.jl

d₁ = PlotData(
    x = [1, 2, 3],
    y = [4, 1, 2],
    plot = StipplePlotly.Charts.PLOT_TYPE_BAR,
    name = "Barcelona")
    
d₂ = PlotData(
    x = [1, 2, 3],
    y = [2, 4, 5],
    plot = StipplePlotly.Charts.PLOT_TYPE_BAR,
    name = "London")

and there's the handlers

@handlers begin
    @out data = [d₁, d₂]
    @out layout = PlotLayout()
end
Author Avatarabhimanyuaryan
4/3/2023, 6:09:21 AM

full example:

using GenieFramework
@genietools

d₁ = PlotData(
  x=[1, 2, 3],
  y=[4, 1, 2],
  plot=StipplePlotly.Charts.PLOT_TYPE_BAR,
  name="Barcelona")

d₂ = PlotData(
  x=[1, 2, 3],
  y=[2, 4, 5],
  plot=StipplePlotly.Charts.PLOT_TYPE_BAR,
  name="London")

@handlers begin
  @out data = [d₁, d₂]
  @out layout = layout = PlotLayout(
    xaxis=[PlotLayoutAxis(xy="x", title="my_x_axis")],
    yaxis=[PlotLayoutAxis(xy="y", title="density")])
end

function ui()
  [
    h1("GenieFramework 🧞 Data Vizualization 📊")
    plot(:data, layout=:layout)
  ]
end

@page("/", ui)

Server.isrunning() || Server.up()

cd /TestZip

$ julia --project
julia> using Genie
julia> Genie.loadapp()

go to local_ip:port and you'll see the changes

Author Avatarabhimanyuaryan
4/3/2023, 6:13:34 AM

@itzi you can check more JS examples here: https://plotly.com/javascript/

PlotData only takes data. JavaScript examples below. You can convert them into Julia

remove var i.e. var trace1 becomes trace1. Object {} becomes julia object/struct PlotData(....)

var trace1 = {
  x: [1, 2, 3, 4],
  y: [10, 15, 13, 17],
  type: 'scatter'
};

var trace2 = {
  x: [1, 2, 3, 4],
  y: [16, 5, 11, 9],
  type: 'scatter'
};

var data = [trace1, trace2];

Plotly.newPlot('myDiv', data);

and

var trace1 = {
  x: [1, 2, 3, 4],
  y: [10, 15, 13, 17],
  mode: 'markers',
  marker: {
    color: 'rgb(219, 64, 82)',
    size: 12
  }
};

var trace2 = {
  x: [2, 3, 4, 5],
  y: [16, 5, 11, 9],
  mode: 'lines',
  line: {
    color: 'rgb(55, 128, 191)',
    width: 3
  }
};

var trace3 = {
  x: [1, 2, 3, 4],
  y: [12, 9, 15, 12],
  mode: 'lines+markers',
  marker: {
    color: 'rgb(128, 0, 128)',
    size: 8
  },
  line: {
    color: 'rgb(128, 0, 128)',
    width: 1
  }
};

var data = [trace1, trace2, trace3];
Author Avatarabhimanyuaryan
4/3/2023, 6:15:39 AM

i.e.

var layout = {
  width: 500,
  height: 500
};

turns

my_layout = PlotLayout(width=500, height=500)
Author Avatarabhimanyuaryan
4/3/2023, 6:16:17 AM

and you turn any julia variable into reactive variables using @out in @app (new API) or @handlers(old api)

Author Avatarabhimanyuaryan
4/3/2023, 6:16:41 AM
@app begin
  @in data = [d₁, d₂]
  @in layout = layout = PlotLayout(
    xaxis=[PlotLayoutAxis(xy="x", title="my_x_axis")],
    yaxis=[PlotLayoutAxis(xy="y", title="density")])
end
Author Avatarabhimanyuaryan
4/3/2023, 6:22:44 AM

^ the above is then created into vue data

"data":{"isready":false,"isprocessing":false,"data":[{"y":[4,1,2],"type":"bar","name":"Barcelona","x":[1,2,3]},{"y":[2,4,5],"type":"bar","name":"London","x":[1,2,3]}],"layout":{"xaxis":{"title":"my_x_axis"},"yaxis":{"title":"density"}}}});

Let me know if any issue understanding

Author Avataritsdfish
4/3/2023, 6:05:13 PM

@abhimanyuaryan, thank you so much for walking me through these examples. This is very helpful.

I will give it a try soon and let you know if there are any issues.

Please let me know if your example can be leveraged for documentation. I would be willing to open an issue and add it to the documentation if I can figure it out (I had trouble getting Documenter.jl to work in the past).

Author AvatarPere
4/3/2023, 8:02:04 PM

@itsdfish that would be great! We recently created a repository for short code examples that people can copypaste. You can add yours there in src/2.reactive-ui if you'd like

https://github.com/GenieFramework/CodeExamples

Author Avataritsdfish
4/3/2023, 8:25:43 PM

Thank you. I checkout the example abhi created tomorrow morning and add it to the list of examples in the evening.

Author Avataritsdfish
4/3/2023, 10:46:08 PM

Thank you again. I was able to simplify the code and figure out how to manually specify the range, turn off the grid and a few other things. I will add the example to the repo tomorrow.

I have one question moving forward: how do I specify plot(:data, layout=:layout) in an html file? I see that I can use <plotly :data="pdf_plot"></plotly> for PlotData, but what should I use in this case?

Author Avatarabhimanyuaryan
4/4/2023, 1:46:09 AM
<template>
  <div>
    <plotly :data="data" :layout="layout" :config="config" />
  </div>
</template>

<script>
import Plotly from 'vue-plotly';
export default {
  components: { Plotly },
  data() {
    return {
      data: [
        {
          x: [1, 2, 3, 4],
          y: [10, 11, 12, 13],
          type: 'scatter'
        }
      ],
      layout: {
        title: 'My Plot'
      },
      config: {}
    };
  }
};
</script>

here's an example from vue and plotly html tag. This is should work

Author Avatarabhimanyuaryan
4/4/2023, 1:47:25 AM
<plotly :data="pdf_plot" :layout="my_layout" :config="my_config" />

let me know incase any issues

Author Avataritsdfish
4/4/2023, 8:06:12 PM

Thank you again for all of your help. Here is a link to the PR in case someone comes across this thread and wants a self-contained example.

https://github.com/GenieFramework/CodeExamples/pull/3