enkr1

enkr1

Hi community,

I am trying to plot a line graph with Elixir’s ContEx charting system. I’m assigning the dataset with Dataset.new/1, before assigning the chart with LinePlot.new/2 and rendering the SVG with Plot.new/3. The data I’m passing into Dataset.new/1 is formatted as {x_value, y_value}.

[
	{1, 10.1},
	{2, 10.1},
	{3, 8.5},
	{4, 6.0},
	{5, 0.0},
	{6, 0.0},
	{7, 0.0},
	{8, 0.0},
	{9, 0.0},
	{10, 0.0}
]

---

The issue is that the plotted line graph will either dip below or rise above the actual value. Eg. The line rises slightly from x=1 to x=2 even though both y-values should be 10.1. Similarly, the line dips slightly below 0 from x=5 to x=6, even though both y-values should be 0.


When I use the same data to plot a bar chart with BarChart.new/2 instead of LinePlot.new/2, the chart appears to be correct.


Here are the code snippets that may be causing the issue.

barchart_helper.ex:

defmodule MyWeb.Helpers.BarchartHelper do
  alias Contex.{Dataset, BarChart, Plot, LinePlot}

  def make_bar_chart_dataset(data), do: Dataset.new(data)

  def make_pipeline_chart(dataset) do
    LinePlot.new(dataset) # or BarChart.new(dataset) to plot a barchart
  end

  def render_bar_chart(chart, title, subtitle, x_axis, y_axis) do
    Plot.new(500, 400, chart)
    |> Plot.titles(title, subtitle)
    |> Plot.axis_labels(x_axis, y_axis)
    |> Plot.to_svg()
  end
end

test_chart.ex:

defmodule MyWeb.TestLive.TestChart do
  use MyWeb, :live_component
  use MyWeb, :chart_live

  def update(%{total_resources: total_resources} = assigns, socket) do
    {:ok,
     socket
     |> assign(assigns)
     |> assign(:total_resources, total_resources)
     |> assign_dataset()
     |> assign_chart()
     |> assign_chart_svg()}
  end

  def assign_dataset(%{assigns: %{total_resources: total_resources}} = socket) do
    socket |> assign(:dataset, make_bar_chart_dataset(total_resources))
  end

  def assign_chart(%{assigns: %{dataset: dataset}} = socket) do
    socket |> assign(:chart, make_pipeline_chart(dataset))
  end

  def assign_chart_svg(%{assigns: %{chart: chart}} = socket) do
    socket
    |> assign(
      :chart_svg,
      render_bar_chart(chart, "Title", "Subtitle", "X axis", "Y axis")
    )
  end

  def render(assigns) do
    ~H"""
    <div>
      <%= @chart_svg %>
    </div>
    """
  end
end

Thank you in advance.

Best wishes,
Jing Hui P.

Marked As Solved

enkr1

enkr1 OP

Thank you @hauleth @mindok!

def make_pipeline_chart(dataset) do
  options = [
    data_labels: false,
    smoothed: false,
    custom_x_formatter: &my_fn/1
  ]

  LinePlot.new(dataset, options)
end

Cheers! :heart:

Also Liked

hauleth

hauleth

Disable smoothing as this is what is causing that visual glitch. If you look closely you will see that the data is still the same, just the visual representation of interpolated data “in between” the points is visually below 0.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews