We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Text Align (textAlign) Not Working

Hi,

I am unable to get the textAlign to work at all.

Here's my code:

function AlignText(alignment) {
    for (var i = 0; i < diagram.model.selectedItems.children.length; i++) {
        var object = diagram.model.selectedItems.children[i];
        if (object.type == 'node') { // A node or a text object
            if (object.shape.type == 'text') { // text object
                if (object.shape.textBlock) {
                    diagram.updateLabel(object.name, object.shape.textBlock, { textAlign: alignment });
                }
            } else { // node
                if (object.labels[0]) {
                    diagram.updateLabel(object.name, object.labels[0], { textAlign: alignment });
                }
            }
        } else { // Must be a connector
            if (object.labels[0]) {
                diagram.updateLabel(object.name, object.labels[0], { textAlign: alignment });
            }
        }
    }
}

I've put alerts in the above so I know it is being called.
I am using almost identical code to set other properties such as fontFamily and that works fine.

What am I doing wrong?

Thanks as usual.

Jim

4 Replies

JJ Jim Jacobs February 8, 2015 09:34 PM UTC

Hi again,

Just in case you're wondering, I am passing one of three values to my function:
  left, right or center (all lowercase)

Jim


SG Shyam G Syncfusion Team February 9, 2015 01:24 PM UTC

Hi Jim

Thanks for using Syncfusion products.

We suggest you to set an “offset for an label of text node in order to achieve your requirement and you can define the label offset value from 0 to 1. Please refer the code snippet and sample below.

Code snippet:

function updatelabel() {            

            var diagram = $("#Diagram1").ejDiagram("instance");

            var node = diagram.selectionList[0];

            if (node) {

                if (node.shape.textBlock) {                 

                    var obj = new Object();

                    obj.textAlign = "left";

                    //offset for left align

                    obj.offset = ej.datavisualization.Diagram.Point(0, 0.5);  

                    //update text node label

                    diagram.updateLabel(node.name, node.shape.textBlock, obj);

                }

            }

        }

Sample: http://www.syncfusion.com/downloads/support/directtrac/118174/WebApplication2905723546.zip

Please let me know if any concerns.

Regards,

Shyam G




JJ Jim Jacobs February 9, 2015 04:31 PM UTC

Hi Shyam,

Thanks for the code.
It got me going in the right direction.
I found I had to tweak the X offset depending on the object type.

For the benefit of others than might be playing with this, here's what I ended up using:

Text object:
  Left:  obj.offset = ej.datavisualization.Diagram.Point(0.30, 0.5);
  Right:  obj.offset = ej.datavisualization.Diagram.Point(0.70, 0.5);

Node:
  Left:  obj.offset = ej.datavisualization.Diagram.Point(0.15, 0.5);
  Right:  obj.offset = ej.datavisualization.Diagram.Point(0.85, 0.5);

Connector:
  Left:  obj.offset = ej.datavisualization.Diagram.Point(0.25, 0.5);
  Right:  obj.offset = ej.datavisualization.Diagram.Point(0.75, 0.5);

And of course the center alignment used:  obj.offset = ej.datavisualization.Diagram.Point(0.5, 0.5); for all object types.

Thanks again.

Jim


SG Shyam G Syncfusion Team February 10, 2015 04:26 AM UTC

Hi Jim

Thanks for the update

Please let me know if you require further assistance on this.

Regards,

Shyam G



Loader.
Up arrow icon