From 3d450ab976652bdc6bd6807dc21eeaff0c914fc8 Mon Sep 17 00:00:00 2001 From: Stamates Date: Sat, 9 Oct 2021 14:53:55 -0400 Subject: [PATCH 1/4] Add Dataset protocol to provide data name and value labels to be used in the accessibility table for a chart. --- uncharted/lib/uncharted.ex | 5 ++-- uncharted/lib/uncharted/dataset.ex | 26 +++++++++++++++++++ .../lib/uncharted/doughnut_chart/dataset.ex | 13 +++++++++- uncharted_phoenix/README.md | 2 ++ .../components/live_doughnut.html.leex | 4 +-- .../uncharted_phoenix/views/component_view.ex | 2 +- 6 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 uncharted/lib/uncharted/dataset.ex diff --git a/uncharted/lib/uncharted.ex b/uncharted/lib/uncharted.ex index 3364ead..d0eb5a3 100644 --- a/uncharted/lib/uncharted.ex +++ b/uncharted/lib/uncharted.ex @@ -9,12 +9,13 @@ defmodule Uncharted do @type color_name :: atom() @typedoc """ - a struct representing the dataset that the chart uses. Dfferent charts needs similar but differing information so this - is a union type + a struct representing the dataset that the chart uses. Dfferent charts need similar but differing + information so this is a union type """ @type dataset :: Uncharted.BarChart.Dataset.t() | Uncharted.ColumnChart.Dataset.t() + | Uncharted.DoughnutChart.Dataset.t() | Uncharted.LineChart.Dataset.t() | Uncharted.PieChart.Dataset.t() | Uncharted.ProgressChart.Dataset.t() diff --git a/uncharted/lib/uncharted/dataset.ex b/uncharted/lib/uncharted/dataset.ex new file mode 100644 index 0000000..98f935e --- /dev/null +++ b/uncharted/lib/uncharted/dataset.ex @@ -0,0 +1,26 @@ +defprotocol Uncharted.Dataset do + @doc """ + Takes an `Uncharted.dataset` data structure + and returns a label for the data names. This label + will be used as the first header of the data table + provided for accessibilty. + """ + @fallback_to_any true + @spec data_name_label(Uncharted.dataset()) :: String.t() + def data_name_label(dataset) + + @doc """ + Takes an `Uncharted.dataset` data structure + and returns a label for the data values. This label + will be used as the second header of the data table + provided for accessibilty. + """ + @fallback_to_any true + @spec data_value_label(Uncharted.dataset()) :: String.t() + def data_value_label(dataset) +end + +defimpl Uncharted.Dataset, for: Any do + def data_name_label(_), do: "Data Names" + def data_value_label(_), do: "Data Values" +end diff --git a/uncharted/lib/uncharted/doughnut_chart/dataset.ex b/uncharted/lib/uncharted/doughnut_chart/dataset.ex index 465e1ec..e205c9f 100644 --- a/uncharted/lib/uncharted/doughnut_chart/dataset.ex +++ b/uncharted/lib/uncharted/doughnut_chart/dataset.ex @@ -4,6 +4,8 @@ defmodule Uncharted.DoughnutChart.Dataset do """ defstruct [ :data, + :data_name_label, + :data_value_label, :center_value, :center_value_fill_color, :label, @@ -13,7 +15,9 @@ defmodule Uncharted.DoughnutChart.Dataset do @typep color :: atom() @type t() :: %__MODULE__{ - data: Uncharted.DoughnutChart.Dataset.t(), + data: list(Uncharted.Datum.t()), + data_name_label: String.t(), + data_value_label: String.t(), center_value: number(), center_value_fill_color: color(), label: String.t() | nil, @@ -21,3 +25,10 @@ defmodule Uncharted.DoughnutChart.Dataset do secondary_label: String.t() | nil } end + +defimpl Uncharted.Dataset, for: Uncharted.DoughnutChart.Dataset do + alias Uncharted.DoughnutChart.Dataset + + def data_name_label(%Dataset{data_name_label: data_name_label}), do: data_name_label + def data_value_label(%Dataset{data_value_label: data_value_label}), do: data_value_label +end diff --git a/uncharted_phoenix/README.md b/uncharted_phoenix/README.md index a543b98..3b58f14 100644 --- a/uncharted_phoenix/README.md +++ b/uncharted_phoenix/README.md @@ -321,6 +321,8 @@ doughnut_chart = %BaseChart{ values: [17.0] } ], + data_name_label: "Doughnut Type", + data_value_label: "Percentage", center_value: 100, center_value_fill_color: :blue_gradient, label: "Donuts Tasted", diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex index 7956f87..8ca241a 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex @@ -50,8 +50,8 @@ <%= Chart.title(@chart) %> - <%= @chart.title %> - Percentage + <%= Dataset.data_name_label(@chart.dataset) %> + <%= Dataset.data_value_label(@chart.dataset) %> <%= for slice <- svg_doughnut_slices(@doughnut_slices) do %> diff --git a/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex b/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex index 0de1d81..327524b 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex +++ b/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex @@ -6,7 +6,7 @@ defmodule UnchartedPhoenix.ComponentView do use Phoenix.HTML - alias Uncharted.{Chart, Gradient} + alias Uncharted.{Chart, Dataset, Gradient} alias Uncharted.ColumnChart.Column alias Uncharted.LineChart.{Line, Point} alias Uncharted.ScatterPlot.Point, as: ScatterPoint From dc852783f177850a74eff53a543e5b738ff3acb7 Mon Sep 17 00:00:00 2001 From: Stamates Date: Sat, 9 Oct 2021 14:58:17 -0400 Subject: [PATCH 2/4] Rename doughnut to donut throughout --- README.md | 2 +- uncharted/lib/uncharted.ex | 2 +- .../uncharted/donut_chart/base_chart_impl.ex | 19 +++++++++++++++++ .../dataset.ex | 8 +++---- .../lib/uncharted/donut_chart/donut_chart.ex | 3 +++ .../donut_slice.ex} | 4 ++-- .../doughnut_chart/base_chart_impl.ex | 19 ----------------- .../doughnut_chart/doughnut_chart.ex | 3 --- .../lib/uncharted/progress_chart/dataset.ex | 4 ++-- .../test/uncharted/doughnut_chart_test.exs | 21 +++++++++---------- uncharted_phoenix/README.md | 16 +++++++------- .../components/component_impl.ex | 4 ++-- .../{live_doughnut.ex => live_donut.ex} | 8 +++---- ...oughnut.html.leex => live_donut.html.leex} | 16 +++++++------- .../components/live_progress.html.leex | 6 +++--- .../uncharted_phoenix/views/component_view.ex | 14 ++++++------- .../components/live_doughnut_test.exs | 10 ++++----- .../test/uncharted_phoenix_test.exs | 10 ++++----- 18 files changed, 84 insertions(+), 85 deletions(-) create mode 100644 uncharted/lib/uncharted/donut_chart/base_chart_impl.ex rename uncharted/lib/uncharted/{doughnut_chart => donut_chart}/dataset.ex (78%) create mode 100644 uncharted/lib/uncharted/donut_chart/donut_chart.ex rename uncharted/lib/uncharted/{doughnut_chart/doughnut_slice.ex => donut_chart/donut_slice.ex} (63%) delete mode 100644 uncharted/lib/uncharted/doughnut_chart/base_chart_impl.ex delete mode 100644 uncharted/lib/uncharted/doughnut_chart/doughnut_chart.ex rename uncharted_phoenix/lib/uncharted_phoenix/components/{live_doughnut.ex => live_donut.ex} (76%) rename uncharted_phoenix/lib/uncharted_phoenix/components/{live_doughnut.html.leex => live_donut.html.leex} (78%) diff --git a/README.md b/README.md index 1786dbe..5eafb1c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ basic chart package Uncharted and adapts Uncharted charts for display as compone ***Charts Included***: - The Bar Chart - The Column Chart -- The Doughnut Chart +- The Donut Chart - The Line Chart - The Pie Chart - The Progress Chart diff --git a/uncharted/lib/uncharted.ex b/uncharted/lib/uncharted.ex index d0eb5a3..ce17ea3 100644 --- a/uncharted/lib/uncharted.ex +++ b/uncharted/lib/uncharted.ex @@ -15,7 +15,7 @@ defmodule Uncharted do @type dataset :: Uncharted.BarChart.Dataset.t() | Uncharted.ColumnChart.Dataset.t() - | Uncharted.DoughnutChart.Dataset.t() + | Uncharted.DonutChart.Dataset.t() | Uncharted.LineChart.Dataset.t() | Uncharted.PieChart.Dataset.t() | Uncharted.ProgressChart.Dataset.t() diff --git a/uncharted/lib/uncharted/donut_chart/base_chart_impl.ex b/uncharted/lib/uncharted/donut_chart/base_chart_impl.ex new file mode 100644 index 0000000..56ea6a1 --- /dev/null +++ b/uncharted/lib/uncharted/donut_chart/base_chart_impl.ex @@ -0,0 +1,19 @@ +defimpl Uncharted.DonutChart, for: Uncharted.BaseChart do + alias Uncharted.BaseChart + alias Uncharted.DonutChart.DonutSlice + + def donut_slices(%BaseChart{dataset: nil}), do: [] + + def donut_slices(%BaseChart{dataset: %{data: []}}), do: [] + + def donut_slices(%BaseChart{dataset: %{data: data}}) do + data + |> Enum.map(fn datum -> + %DonutSlice{ + label: datum.name, + percentage: hd(datum.values), + fill_color: datum.fill_color + } + end) + end +end diff --git a/uncharted/lib/uncharted/doughnut_chart/dataset.ex b/uncharted/lib/uncharted/donut_chart/dataset.ex similarity index 78% rename from uncharted/lib/uncharted/doughnut_chart/dataset.ex rename to uncharted/lib/uncharted/donut_chart/dataset.ex index e205c9f..9109c21 100644 --- a/uncharted/lib/uncharted/doughnut_chart/dataset.ex +++ b/uncharted/lib/uncharted/donut_chart/dataset.ex @@ -1,6 +1,6 @@ -defmodule Uncharted.DoughnutChart.Dataset do +defmodule Uncharted.DonutChart.Dataset do @moduledoc """ - Struct representing a dataset for a basic doughnut chart. + Struct representing a dataset for a basic donut chart. """ defstruct [ :data, @@ -26,8 +26,8 @@ defmodule Uncharted.DoughnutChart.Dataset do } end -defimpl Uncharted.Dataset, for: Uncharted.DoughnutChart.Dataset do - alias Uncharted.DoughnutChart.Dataset +defimpl Uncharted.Dataset, for: Uncharted.DonutChart.Dataset do + alias Uncharted.DonutChart.Dataset def data_name_label(%Dataset{data_name_label: data_name_label}), do: data_name_label def data_value_label(%Dataset{data_value_label: data_value_label}), do: data_value_label diff --git a/uncharted/lib/uncharted/donut_chart/donut_chart.ex b/uncharted/lib/uncharted/donut_chart/donut_chart.ex new file mode 100644 index 0000000..8efd311 --- /dev/null +++ b/uncharted/lib/uncharted/donut_chart/donut_chart.ex @@ -0,0 +1,3 @@ +defprotocol Uncharted.DonutChart do + def donut_slices(chart) +end diff --git a/uncharted/lib/uncharted/doughnut_chart/doughnut_slice.ex b/uncharted/lib/uncharted/donut_chart/donut_slice.ex similarity index 63% rename from uncharted/lib/uncharted/doughnut_chart/doughnut_slice.ex rename to uncharted/lib/uncharted/donut_chart/donut_slice.ex index 141d4f8..b22eb4b 100644 --- a/uncharted/lib/uncharted/doughnut_chart/doughnut_slice.ex +++ b/uncharted/lib/uncharted/donut_chart/donut_slice.ex @@ -1,6 +1,6 @@ -defmodule Uncharted.DoughnutChart.DoughnutSlice do +defmodule Uncharted.DonutChart.DonutSlice do @moduledoc """ - A struct representing doughnut chart slice display properties. + A struct representing donut chart slice display properties. """ defstruct [:label, :percentage, :fill_color] diff --git a/uncharted/lib/uncharted/doughnut_chart/base_chart_impl.ex b/uncharted/lib/uncharted/doughnut_chart/base_chart_impl.ex deleted file mode 100644 index f1befec..0000000 --- a/uncharted/lib/uncharted/doughnut_chart/base_chart_impl.ex +++ /dev/null @@ -1,19 +0,0 @@ -defimpl Uncharted.DoughnutChart, for: Uncharted.BaseChart do - alias Uncharted.BaseChart - alias Uncharted.DoughnutChart.DoughnutSlice - - def doughnut_slices(%BaseChart{dataset: nil}), do: [] - - def doughnut_slices(%BaseChart{dataset: %{data: []}}), do: [] - - def doughnut_slices(%BaseChart{dataset: %{data: data}}) do - data - |> Enum.map(fn datum -> - %DoughnutSlice{ - label: datum.name, - percentage: hd(datum.values), - fill_color: datum.fill_color - } - end) - end -end diff --git a/uncharted/lib/uncharted/doughnut_chart/doughnut_chart.ex b/uncharted/lib/uncharted/doughnut_chart/doughnut_chart.ex deleted file mode 100644 index 10bd97b..0000000 --- a/uncharted/lib/uncharted/doughnut_chart/doughnut_chart.ex +++ /dev/null @@ -1,3 +0,0 @@ -defprotocol Uncharted.DoughnutChart do - def doughnut_slices(chart) -end diff --git a/uncharted/lib/uncharted/progress_chart/dataset.ex b/uncharted/lib/uncharted/progress_chart/dataset.ex index 135356f..3023d58 100644 --- a/uncharted/lib/uncharted/progress_chart/dataset.ex +++ b/uncharted/lib/uncharted/progress_chart/dataset.ex @@ -13,7 +13,7 @@ defmodule Uncharted.ProgressChart.Dataset do :percentage_fill_color, :label_fill_color, progress_shape: :round, - doughnut_width: 5 + donut_width: 5 ] @typep color :: atom() @@ -27,6 +27,6 @@ defmodule Uncharted.ProgressChart.Dataset do percentage_fill_color: color(), label_fill_color: color(), progress_shape: :round | :butt | :square, - doughnut_width: number() + donut_width: number() } end diff --git a/uncharted/test/uncharted/doughnut_chart_test.exs b/uncharted/test/uncharted/doughnut_chart_test.exs index de5a9bc..0ec5b42 100644 --- a/uncharted/test/uncharted/doughnut_chart_test.exs +++ b/uncharted/test/uncharted/doughnut_chart_test.exs @@ -1,8 +1,8 @@ -defmodule Uncharted.DoughnutChartTest do +defmodule Uncharted.DonutChartTest do alias Uncharted.{ BaseChart, BaseDatum, - DoughnutChart + DonutChart } use ExUnit.Case @@ -14,30 +14,29 @@ defmodule Uncharted.DoughnutChartTest do %BaseDatum{name: "Slice Four", values: [7.0]}, %BaseDatum{name: "Slice Five", values: [22.0]} ] - @dataset %DoughnutChart.Dataset{data: @data} + @dataset %DonutChart.Dataset{data: @data} @chart %BaseChart{title: "title", dataset: @dataset} - describe "doughnut_slices/1" do + describe "donut_slices/1" do test "returns the number of slices that make up the dataset" do - assert length(DoughnutChart.doughnut_slices(@chart)) == length(@data) + assert length(DonutChart.donut_slices(@chart)) == length(@data) end - test "returns doughnut slice labels" do - doughnut_slices = Enum.map(DoughnutChart.doughnut_slices(@chart), & &1.label) + test "returns donut slice labels" do + donut_slices = Enum.map(DonutChart.donut_slices(@chart), & &1.label) labels = Enum.map(@data, & &1.name) - assert doughnut_slices + assert donut_slices |> Enum.zip(labels) |> Enum.all?(fn {actual, expected} -> actual == expected end) end test "returns correct percentages for slices" do - doughnut_slice_percentages = - Enum.map(DoughnutChart.doughnut_slices(@chart), & &1.percentage) + donut_slice_percentages = Enum.map(DonutChart.donut_slices(@chart), & &1.percentage) expected_percentages = [20.0, 24.0, 27.0, 7.0, 22.0] - assert doughnut_slice_percentages == expected_percentages + assert donut_slice_percentages == expected_percentages end end end diff --git a/uncharted_phoenix/README.md b/uncharted_phoenix/README.md index 3b58f14..ea97651 100644 --- a/uncharted_phoenix/README.md +++ b/uncharted_phoenix/README.md @@ -2,7 +2,7 @@ A simple ***Elixir*** charting library that generates easy to customize charts for ***Phoenix*** and ***LiveView***. ## Features -- Easily generate pie charts, doughnut charts, column charts, bar charts, progress counters, and line charts +- Easily generate pie charts, donut charts, column charts, bar charts, progress counters, and line charts - Generates responsive and accessible SVGs as LiveView components - Provides advanced styling like gradients and rounded corners - Smooth animations for updating live data to the UI @@ -220,7 +220,7 @@ defp progress_chart(from: %BaseChart{} = chart) do percentage_text_fill_color: :blue_gradient, percentage_fill_color: :rose_gradient, label_fill_color: :rose_gradient, - doughnut_width: 5, + donut_width: 5, progress_shape: :round } } @@ -278,12 +278,12 @@ scatter_plot = %BaseChart{ } ``` -### The Doughnut Chart -![Doughnut Chart](assets/images/doughnut-chart.jpg "Doughnut Chart") +### The Donut Chart +![Donut Chart](assets/images/donut-chart.jpg "Donut Chart") ```elixir -doughnut_chart = %BaseChart{ - title: "Best kind of doughnut", +donut_chart = %BaseChart{ + title: "Best kind of donut", colors: %{ rose_gradient: %Gradient{ start_color: "#642B73", @@ -298,7 +298,7 @@ doughnut_chart = %BaseChart{ stop_color: "#FF1379" } }, - dataset: %DoughnutChart.Dataset{ + dataset: %DonutChart.Dataset{ data: [ %BaseDatum{ name: "Cake", @@ -321,7 +321,7 @@ doughnut_chart = %BaseChart{ values: [17.0] } ], - data_name_label: "Doughnut Type", + data_name_label: "Donut Type", data_value_label: "Percentage", center_value: 100, center_value_fill_color: :blue_gradient, diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/component_impl.ex b/uncharted_phoenix/lib/uncharted_phoenix/components/component_impl.ex index 23c1085..80ae87d 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/component_impl.ex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/component_impl.ex @@ -2,7 +2,7 @@ defimpl Uncharted.Component, for: Uncharted.BaseChart do alias Uncharted.BaseChart alias Uncharted.BarChart alias Uncharted.ColumnChart - alias Uncharted.DoughnutChart + alias Uncharted.DonutChart alias Uncharted.LineChart alias Uncharted.PieChart alias Uncharted.ProgressChart @@ -12,7 +12,7 @@ defimpl Uncharted.Component, for: Uncharted.BaseChart do case dataset do %BarChart.Dataset{} -> UnchartedPhoenix.LiveBarComponent %ColumnChart.Dataset{} -> UnchartedPhoenix.LiveColumnComponent - %DoughnutChart.Dataset{} -> UnchartedPhoenix.LiveDoughnutComponent + %DonutChart.Dataset{} -> UnchartedPhoenix.LiveDonutComponent %LineChart.Dataset{} -> UnchartedPhoenix.LiveLineComponent %PieChart.Dataset{} -> UnchartedPhoenix.LivePieComponent %ProgressChart.Dataset{} -> UnchartedPhoenix.LiveProgressComponent diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.ex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.ex similarity index 76% rename from uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.ex rename to uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.ex index 2d1f2f8..01a6874 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.ex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.ex @@ -1,6 +1,6 @@ -defmodule UnchartedPhoenix.LiveDoughnutComponent do +defmodule UnchartedPhoenix.LiveDonutComponent do @moduledoc """ - Doughnut Chart Component + Donut Chart Component """ use Phoenix.LiveComponent @@ -13,14 +13,14 @@ defmodule UnchartedPhoenix.LiveDoughnutComponent do socket = socket |> assign(:chart, assigns.chart) - |> assign(:doughnut_slices, Uncharted.DoughnutChart.doughnut_slices(assigns.chart)) + |> assign(:donut_slices, Uncharted.DonutChart.donut_slices(assigns.chart)) |> assign(:always_show_table, assigns.always_show_table) {:ok, socket} end def render(assigns) do - Phoenix.View.render(UnchartedPhoenix.ComponentView, "live_doughnut.html", assigns) + Phoenix.View.render(UnchartedPhoenix.ComponentView, "live_donut.html", assigns) end def handle_event("show_table", _, socket) do diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.html.leex similarity index 78% rename from uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex rename to uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.html.leex index 8ca241a..55efe5c 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_doughnut.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_donut.html.leex @@ -1,11 +1,11 @@ -
- " class="doughnut-chart" height="100%" width="100%" viewBox="0 0 20 24" overflow="visible"> - Doughnut chart visualizing <%= Chart.title(@chart) %> +
+ " class="donut-chart" height="100%" width="100%" viewBox="0 0 20 24" overflow="visible"> + Donut chart visualizing <%= Chart.title(@chart) %> " class="chart-data" y="0" x="0" width="100%" height="90%"> - - - <%= for %{remaining_percentage: remaining_percentage, fill_color: fill_color} <- svg_doughnut_slices(@doughnut_slices) do %> + + + <%= for %{remaining_percentage: remaining_percentage, fill_color: fill_color} <- svg_donut_slices(@donut_slices) do %> " class="chart-key" height="10%" width="100%" y="90%" x="0" aria-hidden="true" overflow="visible"> - <%= for %{label: label, fill_color: fill_color, label_width: label_width, label_position: label_position} <- svg_doughnut_slices(@doughnut_slices) do %> + <%= for %{label: label, fill_color: fill_color, label_width: label_width, label_position: label_position} <- svg_donut_slices(@donut_slices) do %> @@ -54,7 +54,7 @@ <%= Dataset.data_value_label(@chart.dataset) %> - <%= for slice <- svg_doughnut_slices(@doughnut_slices) do %> + <%= for slice <- svg_donut_slices(@donut_slices) do %> <%= slice.label %> <%= slice.percentage %> diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex index 8b3f33c..fec51b3 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex @@ -4,7 +4,7 @@ "> - + <%= @progress %>% @@ -17,14 +17,14 @@ diff --git a/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex b/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex index 327524b..626197c 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex +++ b/uncharted_phoenix/lib/uncharted_phoenix/views/component_view.ex @@ -53,20 +53,20 @@ defmodule UnchartedPhoenix.ComponentView do |> Enum.reverse() end - def svg_doughnut_slices(nil), do: [] - def svg_doughnut_slices([]), do: [] + def svg_donut_slices(nil), do: [] + def svg_donut_slices([]), do: [] - def svg_doughnut_slices(doughnut_slices) do - label_width = 100 / Enum.count(doughnut_slices) + def svg_donut_slices(donut_slices) do + label_width = 100 / Enum.count(donut_slices) - doughnut_slices + donut_slices |> Enum.with_index() - |> Enum.reduce([], fn {doughnut_slice, index}, acc -> + |> Enum.reduce([], fn {donut_slice, index}, acc -> remaining_percentage = 100 - Enum.reduce(acc, 0, fn slice, sum -> sum + slice.percentage end) svg_slice = - doughnut_slice + donut_slice |> Map.from_struct() |> Map.put(:remaining_percentage, remaining_percentage) |> Map.put(:label_width, label_width) diff --git a/uncharted_phoenix/test/uncharted_phoenix/components/live_doughnut_test.exs b/uncharted_phoenix/test/uncharted_phoenix/components/live_doughnut_test.exs index caa146a..3757f78 100644 --- a/uncharted_phoenix/test/uncharted_phoenix/components/live_doughnut_test.exs +++ b/uncharted_phoenix/test/uncharted_phoenix/components/live_doughnut_test.exs @@ -1,5 +1,5 @@ -defmodule UnchartedPhoenix.LiveDoughnutComponentTest do - alias Uncharted.DoughnutChart.Dataset +defmodule UnchartedPhoenix.LiveDonutComponentTest do + alias Uncharted.DonutChart.Dataset import UnchartedPhoenix.TestRenderer use ExUnit.Case @@ -13,9 +13,9 @@ defmodule UnchartedPhoenix.LiveDoughnutComponentTest do } } - describe "LiveDoughnut" do - test "renders doughnut" do - assert render_chart(@base_chart) =~ ~s(data-testid="lc-live-doughnut-component") + describe "LiveDonut" do + test "renders donut" do + assert render_chart(@base_chart) =~ ~s(data-testid="lc-live-donut-component") end test "renders a chart's title" do diff --git a/uncharted_phoenix/test/uncharted_phoenix_test.exs b/uncharted_phoenix/test/uncharted_phoenix_test.exs index fee6e8c..89bccac 100644 --- a/uncharted_phoenix/test/uncharted_phoenix_test.exs +++ b/uncharted_phoenix/test/uncharted_phoenix_test.exs @@ -7,7 +7,7 @@ defmodule UnchartedPhoenixTest do BarChart, BaseChart, ColumnChart, - DoughnutChart, + DonutChart, LineChart, PieChart, ProgressChart @@ -65,13 +65,13 @@ defmodule UnchartedPhoenixTest do ~s(data-testid="lc-live-progress-component") end - test "creates doughnut chart components" do - doughnut_chart = %BaseChart{ + test "creates donut chart components" do + donut_chart = %BaseChart{ @base_chart - | dataset: %DoughnutChart.Dataset{data: []} + | dataset: %DonutChart.Dataset{data: []} } - assert render_chart(doughnut_chart) =~ ~s(data-testid="lc-live-doughnut-component") + assert render_chart(donut_chart) =~ ~s(data-testid="lc-live-donut-component") end test "raises an UnchartedPhoenix.ComponentUndefinedError exception when an invalid chart type is given" do From d499938d876f045733aad1c7cc92b8784a879984 Mon Sep 17 00:00:00 2001 From: Stamates Date: Sat, 9 Oct 2021 15:11:11 -0400 Subject: [PATCH 3/4] Wire up data name and value labels for all remaining tables (uses default fallbacks) --- .../lib/uncharted_phoenix/components/live_bar.html.leex | 4 ++-- .../lib/uncharted_phoenix/components/live_column.html.leex | 4 ++-- .../lib/uncharted_phoenix/components/live_line.html.leex | 4 ++-- .../lib/uncharted_phoenix/components/live_pie.html.leex | 2 +- .../lib/uncharted_phoenix/components/live_progress.html.leex | 2 +- .../lib/uncharted_phoenix/components/live_scatter.html.leex | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_bar.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_bar.html.leex index d905a2b..51efed5 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_bar.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_bar.html.leex @@ -19,8 +19,8 @@ <%= Chart.title(@chart) %> - [Data Title] - [Data Title] + <%= Dataset.data_name_label(@chart.dataset) %> + <%= Dataset.data_value_label(@chart.dataset) %> <%= for bar <- @bars do %> diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_column.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_column.html.leex index 167b436..5e52a8f 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_column.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_column.html.leex @@ -19,8 +19,8 @@ <%= Chart.title(@chart) %> - [Data Title] - [Data Title] + <%= Dataset.data_name_label(@chart.dataset) %> + <%= Dataset.data_value_label(@chart.dataset) %> <%= for column <- @columns do %> diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_line.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_line.html.leex index 07979d6..167faf4 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_line.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_line.html.leex @@ -59,8 +59,8 @@ Point - [Data Title] - [Data Title] + <%= Dataset.data_name_label(@chart.dataset) %> + <%= Dataset.data_value_label(@chart.dataset) %> <%= for {point, index} <- Enum.with_index(@points, 1) do %> diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_pie.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_pie.html.leex index c91beb0..91782e0 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_pie.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_pie.html.leex @@ -37,7 +37,7 @@ <%= Chart.title(@chart) %> - [Data title] + <%= Dataset.data_name_label(@chart.dataset) %> Percentage diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex index fec51b3..df3679e 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_progress.html.leex @@ -40,7 +40,7 @@ <%= @chart.title %> - [Data title] + <%= Dataset.data_name_label(@chart.dataset) %> Progress diff --git a/uncharted_phoenix/lib/uncharted_phoenix/components/live_scatter.html.leex b/uncharted_phoenix/lib/uncharted_phoenix/components/live_scatter.html.leex index 28f1323..7a4c3e2 100644 --- a/uncharted_phoenix/lib/uncharted_phoenix/components/live_scatter.html.leex +++ b/uncharted_phoenix/lib/uncharted_phoenix/components/live_scatter.html.leex @@ -45,8 +45,8 @@ Point - [Data Title] - [Data Title] + <%= Dataset.data_name_label(@chart.dataset) %> + <%= Dataset.data_value_label(@chart.dataset) %> <%= for {point, index} <- Enum.with_index(@points, 1) do %> From 81bc1c0a5ac742a9ae89179f3db46ff6de0a0a84 Mon Sep 17 00:00:00 2001 From: Stamates Date: Sat, 9 Oct 2021 15:25:06 -0400 Subject: [PATCH 4/4] fix dialyzer error --- uncharted/lib/uncharted/donut_chart/dataset.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uncharted/lib/uncharted/donut_chart/dataset.ex b/uncharted/lib/uncharted/donut_chart/dataset.ex index 9109c21..43cf127 100644 --- a/uncharted/lib/uncharted/donut_chart/dataset.ex +++ b/uncharted/lib/uncharted/donut_chart/dataset.ex @@ -15,7 +15,7 @@ defmodule Uncharted.DonutChart.Dataset do @typep color :: atom() @type t() :: %__MODULE__{ - data: list(Uncharted.Datum.t()), + data: list(Uncharted.BaseDatum.t()), data_name_label: String.t(), data_value_label: String.t(), center_value: number(),