@@ -12,7 +12,6 @@ defmodule Matplotex.Figure.Cast do
1212 alias Matplotex.Figure.Coords
1313 alias Matplotex.Figure
1414 alias Matplotex.Figure.Dataset
15- @ tickline_offset 5 / 96
1615 @ xtick_type "figure.x_tick"
1716 @ ytick_type "figure.y_tick"
1817 @ stroke_grid "#ddd"
@@ -326,85 +325,6 @@ defmodule Matplotex.Figure.Cast do
326325 % Figure { figure | axes: % { axes | element: element } }
327326 end
328327
329- def cast_xticks (
330- % Figure {
331- axes:
332- % module {
333- tick: % { x: x_ticks } ,
334- limit: % { x: { _min , _max } = xlim } ,
335- size: { width , _height } ,
336- data: { x_data , y_data } ,
337- dataset: dataset ,
338- element: elements ,
339- show_x_ticks: true ,
340- coords: % Coords { bottom_left: { blx , bly } , x_ticks: { _xtx , xty } } = coords
341- } = axes ,
342- rc_params: % RcParams { x_tick_font: tick_font , x_padding: x_padding }
343- } = figure
344- )
345- when is_list ( x_ticks ) do
346- # TODO: Only if it has to confine
347- x_ticks = confine_ticks ( x_ticks , xlim )
348- x_data = confine_data ( x_data , xlim )
349- dataset = confine_data ( dataset , xlim , :x )
350-
351- { xtick_elements , vgridxs } =
352- Enum . map ( x_ticks , fn tick ->
353- { tick_position , label } =
354- plotify_tick (
355- module ,
356- tick ,
357- xlim ,
358- width - width * x_padding * 2 ,
359- blx + width * x_padding ,
360- x_data ,
361- :x
362- )
363-
364- label =
365- % Label {
366- type: @ xtick_type ,
367- x: tick_position ,
368- y: xty ,
369- text: label
370- }
371- |> merge_structs ( tick_font )
372-
373- # TODO: find a mechanism to pass custom font for ticks
374- line = % Line {
375- type: @ xtick_type ,
376- x1: tick_position ,
377- y1: bly ,
378- x2: tick_position ,
379- y2: bly - @ tickline_offset
380- }
381-
382- { % Tick { type: @ xtick_type , tick_line: line , label: label } , tick_position }
383- end )
384- |> Enum . unzip ( )
385-
386- elements = elements ++ xtick_elements
387- vgrids = Enum . map ( vgridxs , fn g -> { g , bly } end )
388-
389- % Figure {
390- figure
391- | axes: % {
392- axes
393- | data: { x_data , y_data } ,
394- dataset: dataset ,
395- element: elements ,
396- coords: % { coords | vgrids: vgrids }
397- }
398- }
399- end
400-
401- def cast_xticks ( % Figure { axes: % { tick: % { x: _ } , limit: % { x: nil } , show_x_ticks: true } } = figure ) do
402- figure
403- |> set_xlim_from_ticks ( )
404- |> cast_xticks ( )
405- end
406-
407- def cast_xticks ( % Figure { } = figure ) , do: figure
408328
409329 def cast_xticks_by_region (
410330 % Figure {
@@ -507,86 +427,6 @@ defmodule Matplotex.Figure.Cast do
507427 @ lowest_tick |> Nx . linspace ( axis_size , n: number_of_ticks_required ) |> Nx . to_list ( )
508428 end
509429
510- @ spec cast_yticks ( Matplotex.Figure . t ( ) ) :: Matplotex.Figure . t ( )
511- def cast_yticks (
512- % Figure {
513- axes:
514- % module {
515- tick: % { y: y_ticks } ,
516- size: { _width , height } ,
517- element: elements ,
518- coords: % Coords { bottom_left: { blx , bly } , y_ticks: { ytx , _yty } } = coords ,
519- limit: % { y: { _min , _max } = ylim } ,
520- data: { x_data , y_data } ,
521- dataset: dataset ,
522- show_y_ticks: true
523- } = axes ,
524- rc_params: % RcParams { y_tick_font: tick_font , y_padding: padding }
525- } = figure
526- ) do
527- y_ticks = confine_ticks ( y_ticks , ylim )
528- y_data = confine_data ( y_data , ylim )
529- dataset = confine_data ( dataset , ylim , :y )
530-
531- { ytick_elements , hgridys } =
532- Enum . map ( y_ticks , fn tick ->
533- { tick_position , label } =
534- plotify_tick (
535- module ,
536- tick ,
537- ylim ,
538- height - height * padding * 2 ,
539- bly + height * padding ,
540- y_data ,
541- :y
542- )
543-
544- label =
545- % Label {
546- type: @ ytick_type ,
547- y: tick_position ,
548- x: ytx ,
549- text: label ,
550- text_anchor: "end" ,
551- dominant_baseline: "middle"
552- }
553- |> merge_structs ( tick_font )
554-
555- # TODO: find a mechanism to pass custom font for ticks
556- line = % Line {
557- type: @ ytick_type ,
558- y1: tick_position ,
559- x1: blx ,
560- x2: blx - @ tickline_offset ,
561- y2: tick_position
562- }
563-
564- { % Tick { type: @ ytick_type , tick_line: line , label: label } , tick_position }
565- end )
566- |> Enum . unzip ( )
567-
568- elements = elements ++ ytick_elements
569- hgrids = Enum . map ( hgridys , fn g -> { blx , g } end )
570-
571- % Figure {
572- figure
573- | axes: % {
574- axes
575- | data: { x_data , y_data } ,
576- dataset: dataset ,
577- element: elements ,
578- coords: % { coords | hgrids: hgrids }
579- }
580- }
581- end
582-
583- def cast_yticks ( % Figure { axes: % { tick: % { y: _ } , limit: % { y: nil } , show_y_ticks: true } } = figure ) do
584- figure
585- |> set_ylim_from_ticks ( )
586- |> cast_yticks ( )
587- end
588-
589- def cast_ytick ( % Figure { } = figure ) , do: figure
590430
591431 def cast_yticks_by_region (
592432 % Figure {
@@ -853,23 +693,6 @@ defmodule Matplotex.Figure.Cast do
853693
854694 def cast_legends ( figure ) , do: figure
855695
856- defp plotify_tick ( module , { label , value } , lim , axis_size , transition , data , axis ) do
857- { module . plotify ( value , lim , axis_size , transition , data , axis ) , label }
858- end
859-
860- defp plotify_tick ( module , value , lim , axis_size , transition , data , axis ) do
861- { module . plotify ( value , lim , axis_size , transition , data , axis ) , value }
862- end
863-
864- defp min_max ( [ { _pos , _label } | _ ] = ticks ) do
865- ticks
866- |> Enum . min_max_by ( fn { _labe , pos } -> pos end )
867- |> then ( fn { { _label_min , pos_min } , { _label_max , pos_max } } -> { pos_min , pos_max } end )
868- end
869-
870- defp min_max ( ticks ) do
871- Enum . min_max ( ticks )
872- end
873696
874697 defp calculate_center ( % Coords { bottom_left: bottom_left , bottom_right: bottom_right } , { x , y } , :x ) do
875698 { calculate_distance ( bottom_left , bottom_right ) / 2 + x , y }
@@ -899,24 +722,6 @@ defmodule Matplotex.Figure.Cast do
899722
900723 defp rotate_label ( :y ) , do: - 90
901724
902- defp set_xlim_from_ticks ( % Figure { axes: % module { tick: % { x: xtick } } = axes } = figure ) do
903- { xmin , xmax } = min_max ( xtick )
904-
905- xscale = xmax |> round ( ) |> div ( length ( xtick ) - 1 )
906-
907- xlim = { round ( xmin - xscale ) , round ( xmax + xscale ) }
908- axes = module . set_limit ( axes , { :x , xlim } )
909-
910- % Figure { figure | axes: axes }
911- end
912-
913- defp set_ylim_from_ticks ( % Figure { axes: % module { tick: % { y: ytick } } = axes } = figure ) do
914- { ymin , ymax } = min_max ( ytick )
915- yscale = ymax |> round ( ) |> div ( length ( ytick ) - 1 )
916- ylim = { round ( ymin - yscale ) , round ( ymax + yscale ) }
917- axes = module . set_limit ( axes , { :y , ylim } )
918- % Figure { figure | axes: axes }
919- end
920725
921726 defp confine_ticks ( [ { _l , _v } | _ ] = ticks , { min , max } ) do
922727 ticks
0 commit comments