Nitin
Nitin

Reputation: 11

How to create Charts using c# Bot Framework?

I am trying to create some charts(line graph etc.,) in a bot. I have the data available from an online source (finance.Yahoo.com)

I am not sure what library I should be using for this purpose
1. What chart library?
2. What type of Card (Hero Card, Adaptive Card ...) should I use?
3. My current solution which is connecting to external site (Yahoo) is a web api and I am using C# bot framework?

Could anyone share an example on how can I achieve this?

I have tried to use UI.Visualization.Chart library but I wasn't successful in getting the solution. Hence I am looking for some help in this area

Upvotes: 1

Views: 1308

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

Interactive charts are not currently supported in BotFramework, you can use some third-party libs as @Peter Bons suggested or on-line services to generate your chart, but you will need to try to render it as an image file and attach this file to your message using HeroCard/ AdaptiveCard in bot.

Since your data is in finance.Yahoo.com, I'm not sure if Yahoo supports to generate chart image, if not, you will need to get the data from Yahoo and then try to find an online-service or third-party lib to draw the chart image first.

After the image is rendered, you can send it as image attachment for example like this:

[Serializable]
public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        var card = CreateHeroCard();
        Attachment attachment = card.ToAttachment();
        var message = context.MakeMessage();
        message.Attachments.Add(attachment);

        await context.PostAsync(message);

        context.Wait(MessageReceivedAsync);
    }

    private HeroCard CreateHeroCard()
    {
        List<CardImage> cardImages = new List<CardImage>();
        cardImages.Add(new CardImage("your chart image url goes here"));
        var card = new HeroCard()
        {
            Title = "Months with Numbers Bar Chart",
            Subtitle = "Using a Chart as Image service...",
            Text = "Build and connect intelligent bots that have charts rendered as images.",
            Images = cardImages
        };

        return card;
    }
}

Upvotes: 3

Related Questions