Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

[FEATURE] Title alignment Left or Right options

Open SmashedBird opened this issue 9 years ago • 35 comments

In v2 the title options for "position" are top,right,left,bottom. It would be nice to have an option for textAlign (left, center,right). Currently, by default textAlign is center.

For exemple, I would like to have my text at the top [position:'top'] aligned to the left [textAlign:'left'] instead of center.

SmashedBird avatar Dec 14 '16 15:12 SmashedBird

@SmashedBird. I am working on this, I will PR soon.

When PR comes in:

We should be able to offer textAlign option with these selections: left, right, center 😄

Jareechang avatar Feb 19 '17 06:02 Jareechang

@Jareechang in the interests of consistency, is it possible to also support alignment for vertical legends (top, bottom, center). If so, is it possible to use a single enum of values that are direction agnostic?

etimberg avatar Feb 19 '17 13:02 etimberg

support alignment for vertical legends Yes, the text alignment will be supported for different positions (top, right, left, bottom). Is that what you mean ?

single enum of values can you expand on what you mean by single enum of values ?

Jareechang avatar Feb 19 '17 22:02 Jareechang

Yup, that's what I meant. For the top and bottom positions the text could be left, center, or right but for the left and right positions the options would be top, center, bottom.

For the enum, I was thinking 3 values start, center, end that would translate as follows

Value Title Position Alignment
start top / bottom Left
start left / right Top
center * center
end top / bottom Right
end left / right Bottom

etimberg avatar Feb 19 '17 23:02 etimberg

ah I see, thanks for expanding. That makes sense. I'll need to come up with a strategy for checking title Position with the enums.

Jareechang avatar Feb 20 '17 02:02 Jareechang

hey @etimberg, looking over the enum values, I am having a difficult time implementing it with those values.

the position alignment is based on unique X and Y value for vertical or horizontal positions.

Alternatives

1. Add more enum values May I suggest expanding values to include a new category of positions (Vertical or horizontal):

Value Title position Alignment
start_vertical top / bottom Left
start_horizontal left / right Top

....

positions: start_horizontal, start_vertical, end_vertical, end_horizontal

2. Nested K-V pairs This is an example, but i'll most likely write this in a module with public methods for matching vertical or horizontal.

var positionToAlignment = {
    vertical: {
        top: {
            x: ...,
            y: ...
        },
        bottom: {
           ...
        },
        center: {
            ....
        }
    },
    horizontal: {
        left: {
            ...
        },
        right: {
            ...
        },
        center: {
            ...
        }
    },
};

Let me know what you think.

Jareechang avatar Feb 22 '17 16:02 Jareechang

@Jareechang in your proposal, what would happen if the user set start_horizontal but the legend was on the left or the right side?

etimberg avatar Feb 22 '17 23:02 etimberg

well, in the case, we would most likely need to enforce / validate that in our "pattern matcher" when getting the enum values.

Steps:

  1. Take in user inputs
  2. Pattern matcher — match inputs to correct enum values
  3. Get X and Y values from given enum values

rough Example: Matcher

match -> 
   isHorizontal ? 
        isValid ? 
             returnValueBasedOnAlignment
   isVertical ? 
         isValid ?
              returnValueBasedOnAlignment
input: { titlePosition: 'top / bottom', alignment: 'Left' } 
    -> START_HORIZONTAL

input: { titlePosition: 'left / right', alignment: 'Left' } 
    -> CENTER (default to center because invalid user input)

Jareechang avatar Feb 23 '17 15:02 Jareechang

@Jareechang if we need a method that returns x and y for position and align, it's fine to have these two arguments, no need for extra options values. This is a simple case and I don't think we need any JavaScript "kind-of" enum, let's compare strings since it should not be used at so many places.

These options should satisfy @SmashedBird and @etimberg requests (consistent with this proposal):

  • title.position: position of the box around the chart (top, left, right, bottom)
  • title.align: title alignment inside the layout box (start, center, end, stretch)
  • title.textAlign: horizontal alignment of text (left, center and right)

title.position is already implemented and textAlign seems directly mapped to context.textAlign.

image

simonbrunel avatar Feb 23 '17 21:02 simonbrunel

Awesome. Thank you for the good work @simonbrunel

SmashedBird avatar Feb 24 '17 14:02 SmashedBird

quick question @simonbrunel or @etimberg. I am trying to implement this solution but I am blocked by some implementation.

Both of these use the me.margins property.

Align Left: screen shot 2017-03-11 at 1 04 38 pm

Align Right (works fine):

screen shot 2017-03-11 at 1 04 25 pm

is me.margins.left mis-calculated ? or maybe I need to account for Yscale offset ?

Jareechang avatar Mar 11 '17 21:03 Jareechang

hello. I still can't find this feature in latest documentation. Is it available or not?

stranger08 avatar Aug 16 '17 08:08 stranger08

Yes this would be a very nice feature. But until it is made available: is there any way of showing either the y-axis title or the Chart-Title at the top left position, simpy written horizontally (i.e. left-to-right) ? Currently I have the problem that the y-axis title is always shown as rotated-text (i.e. bottom-to-top) and the Chart-Title is shown at the center. Thanks.

Hintalo avatar Sep 06 '17 09:09 Hintalo

Although this feature is called "Title alignment options" please implement the same for the Legend. I need the Legend to be right-aligned on top. Thanks.

sizilium avatar Nov 15 '17 16:11 sizilium

Can a more flexible text positioning be added, I have a requirement for a certain format to be replicated and it would be useful to able to attach several text elements at several positions with multiline and size/font/colour formatting. Similar to my crudely drawn example.

mrdansk avatar Jan 17 '18 09:01 mrdansk

Any updates on this? @Jareechang did you made any progress with https://github.com/chartjs/Chart.js/issues/3706?_pjax=%23js-repo-pjax-container#issuecomment-285900148 ? This is a really nice feature to have within Chartjs

Hedva avatar Apr 25 '18 07:04 Hedva

I am also interested in text-alignment of both the legend items as well as the chart title. Does anyone know whether progress has been made on any of these two possible configurations?

Koshux avatar May 08 '18 07:05 Koshux

+1

heytimc avatar Jun 06 '18 23:06 heytimc

+1

Hantzley avatar Jun 07 '18 08:06 Hantzley

+1

BWagener avatar Jun 11 '18 15:06 BWagener