C#示例

C#示例#

class Person
{
    public string Title {get;set;}
    public string Name {get;set;}
}
display(new Person {Title = "Captain", Name = "Marvel"})
Submission#3+Person
Title
Captain
Name
Marvel
var groupofheroes = new []
{
    new Person {Title = "Captain", Name = "Marvel"},
    new Person {Title = "General", Name = "Okoye"},
    new Person {Title = "Team Lead", Name = "Romanova"},
    new Person {Title = "Lead Engineer", Name= "Washington"}
};
display(groupofheroes);
indexvalue
0
Submission#3+Person
Title
Captain
Name
Marvel
1
Submission#3+Person
Title
General
Name
Okoye
2
Submission#3+Person
Title
Team Lead
Name
Romanova
3
Submission#3+Person
Title
Lead Engineer
Name
Washington
display(HTML("<b style=\"color:#512bd4\">Hello!</b>"));
Hello!
warning CS1701: 假定“Microsoft.DotNet.Interactive”使用的程序集引用“Microsoft.AspNetCore.Html.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”与“Microsoft.AspNetCore.Html.Abstractions”的标识“Microsoft.AspNetCore.Html.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”匹配,您可能需要提供运行时策略
# r "nuget:Octokit, 0.32.0"
# r "nuget:NodaTime, 2.4.6"
using Octokit; using NodaTime; 
using NodaTime.Extensions; 
using XPlot.Plotly; 
(1,1): error CS0006: 未能找到元数据文件“nuget:Octokit, 0.32.0”

(2,1): error CS0006: 未能找到元数据文件“nuget:NodaTime, 2.4.6”

(3,7): error CS0246: 未能找到类型或命名空间名“Octokit”(是否缺少 using 指令或程序集引用?)

(3,22): error CS0246: 未能找到类型或命名空间名“NodaTime”(是否缺少 using 指令或程序集引用?)

(4,7): error CS0246: 未能找到类型或命名空间名“NodaTime”(是否缺少 using 指令或程序集引用?)

(5,7): error CS0246: 未能找到类型或命名空间名“XPlot”(是否缺少 using 指令或程序集引用?)

#r "nuget: Plotly.NET, 2.0.0-preview.6"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.6"

using Plotly.NET;

var xData = response.Results.Select(x => x.Date).ToList();
var yData = response.Results.Select(x => x.DailyVaccinations == null ? 0 : (decimal)(x.DailyVaccinations)).ToList();

GenericChart.GenericChart chart = Chart.Point<DateTime,decimal, string>(x: xData, y: yData, Name: "");
chart
    .WithTraceName("Daily Vaccinations", true)
    .WithX_AxisStyle(title: "Vaccinations", Showgrid: false, Showline: true)
    .WithY_AxisStyle(title: "Date", Showgrid: false, Showline: true);
display(chart);
Installed Packages
  • Plotly.NET, 2.0.0-preview.6
  • Plotly.NET.Interactive, 2.0.0-preview.6
System.IO.FileLoadException: Assembly with same name is already loaded
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromAssemblyFile(FileInfo assemblyFile, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 106
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromDllsInDirectoryAsync(DirectoryInfo directory, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 85
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromDirectoryAsync(DirectoryInfo directory, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 63
   at Microsoft.DotNet.Interactive.App.KernelExtensionLoader.LoadExtensionsFromDirectoryAsync(CompositeKernel kernel, DirectoryInfo extensionDir, KernelInvocationContext context, TelemetrySender telemetrySender) in D:\a\_work\1\s\src\dotnet-interactive\KernelExtensionLoader.cs:line 78
   at Microsoft.DotNet.Interactive.App.KernelExtensionLoader.<>c__DisplayClass0_0.<<UseNuGetExtensions>b__0>d.MoveNext() in D:\a\_work\1\s\src\dotnet-interactive\KernelExtensionLoader.cs:line 53
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.<>c__DisplayClass6_0.<<BuildPipeline>g__Combine|2>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 73
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.<>c__DisplayClass6_0.<<BuildPipeline>g__Combine|2>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 73
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.SendAsync(KernelCommand command, KernelInvocationContext context) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 41
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromAssemblyFile(FileInfo assemblyFile, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 106
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromDllsInDirectoryAsync(DirectoryInfo directory, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 85
   at Microsoft.DotNet.Interactive.App.PackageDirectoryExtensionLoader.LoadFromDirectoryAsync(DirectoryInfo directory, Kernel kernel, KernelInvocationContext context) in D:\a\_work\1\s\src\dotnet-interactive\PackageDirectoryExtensionLoader.cs:line 63
   at Microsoft.DotNet.Interactive.App.KernelExtensionLoader.LoadExtensionsFromDirectoryAsync(CompositeKernel kernel, DirectoryInfo extensionDir, KernelInvocationContext context, TelemetrySender telemetrySender) in D:\a\_work\1\s\src\dotnet-interactive\KernelExtensionLoader.cs:line 78
   at Microsoft.DotNet.Interactive.App.KernelExtensionLoader.<>c__DisplayClass0_0.<<UseNuGetExtensions>b__0>d.MoveNext() in D:\a\_work\1\s\src\dotnet-interactive\KernelExtensionLoader.cs:line 53
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.<>c__DisplayClass6_0.<<BuildPipeline>g__Combine|2>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 73
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.<>c__DisplayClass6_0.<<BuildPipeline>g__Combine|2>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 73
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.KernelCommandPipeline.SendAsync(KernelCommand command, KernelInvocationContext context) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 41
using Plotly.NET;

var xData = response.Results.Select(x => x.Date).ToList();
var yData = response.Results.Select(x => x.DailyVaccinations == null ? 0 : (decimal)(x.DailyVaccinations)).ToList();

GenericChart.GenericChart chart = Chart.Point<DateTime,decimal, string>(x: xData, y: yData, Name: "");
chart
    .WithTraceName("Daily Vaccinations", true)
    .WithX_AxisStyle(title: "Vaccinations", Showgrid: false, Showline: true)
    .WithY_AxisStyle(title: "Date", Showgrid: false, Showline: true);
display(chart);
(3,13): error CS0103: 当前上下文中不存在名称“response”

(4,13): error CS0103: 当前上下文中不存在名称“response”

using System;
using Plotly.NET.CSharp;

Chart.Point<double, double, string>(
    x: new double[] { 1, 2 }, 
    y: new double[] { 5, 10 }
)
.WithTraceInfo("Hello from C#", ShowLegend: true)
.WithXAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("xAxis"))
.WithYAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("yAxis"))
.Show();
(2,18): error CS0234: 命名空间“Plotly.NET”中不存在类型或命名空间名“CSharp”(是否缺少程序集引用?)

(4,1): error CS0103: 当前上下文中不存在名称“Chart”

(9,48): error CS0234: 命名空间“Plotly.NET”中不存在类型或命名空间名“Title”(是否缺少程序集引用?)

(10,48): error CS0234: 命名空间“Plotly.NET”中不存在类型或命名空间名“Title”(是否缺少程序集引用?)

using System;
using Plotly.NET;
using Plotly.NET.LayoutObjects;

double[] x = new double[] { 1, 2 };
double[] y = new double[] { 5, 10 };

LinearAxis xAxis = new LinearAxis();
xAxis.SetValue("title", "xAxis");
xAxis.SetValue("showgrid", false);
xAxis.SetValue("showline", true);

LinearAxis yAxis = new LinearAxis();
yAxis.SetValue("title", "yAxis");
yAxis.SetValue("showgrid", false);
yAxis.SetValue("showline", true);

Layout layout = new Layout();
layout.SetValue("xaxis", xAxis);
layout.SetValue("yaxis", yAxis);
layout.SetValue("showlegend", true);

Trace trace = new Trace("scatter");
trace.SetValue("x", x);
trace.SetValue("y", y);
trace.SetValue("mode", "markers");
trace.SetValue("name", "Hello from C#");

GenericChart
    .ofTraceObject(true, trace)
    .WithLayout(layout)
    .Show();
(3,18): error CS0234: 命名空间“Plotly.NET”中不存在类型或命名空间名“LayoutObjects”(是否缺少程序集引用?)

(8,1): error CS0246: 未能找到类型或命名空间名“LinearAxis”(是否缺少 using 指令或程序集引用?)

(13,1): error CS0246: 未能找到类型或命名空间名“LinearAxis”(是否缺少 using 指令或程序集引用?)

(8,24): error CS0246: 未能找到类型或命名空间名“LinearAxis”(是否缺少 using 指令或程序集引用?)

(13,24): error CS0246: 未能找到类型或命名空间名“LinearAxis”(是否缺少 using 指令或程序集引用?)

(30,6): error CS1501: “ofTraceObject”方法没有采用 2 个参数的重载

#r "nuget: XPlot.Plotly"
#r "nuget: XPlot.Plotly.Interactive"
Installed Packages
  • XPlot.Plotly, 4.0.6
  • XPlot.Plotly.Interactive, 4.0.7
Loading extensions from `C:\Users\richa\.nuget\packages\xplot.plotly.interactive\4.0.7\lib\net7.0\XPlot.Plotly.Interactive.dll`

Configuring PowerShell Kernel for XPlot.Plotly integration.

Installed support for XPlot.Plotly.

using XPlot.Plotly;

var chart = Chart.Plot(
    new Scattergl() // remove Graph.
    {
        x = houses.Select(v => v.Longitude),
        y = houses.Select(v => v.Latitude),
        mode = "markers",
        marker = new Marker() // remove Graph.
        {
            color = houses.Select(v => v.MedianHouseValue),
            colorscale = "Jet"
        }
    }
);
chart.WithXTitle("Longitude");
chart.WithYTitle("Latitude");
chart.WithTitle("Median house value by location");
chart.Width = 600;
chart.Height = 600;
chart // no display call
(6,13): error CS0103: 当前上下文中不存在名称“houses”

(7,13): error CS0103: 当前上下文中不存在名称“houses”

(11,21): error CS0103: 当前上下文中不存在名称“houses”

#r "nuget:Microsoft.Data.Analysis,0.4.0"
Installed Packages
  • Microsoft.Data.Analysis, 0.4.0
using Microsoft.Data.Analysis;

Formatter<DataFrame>.Register((df, writer) =>
{
    var headers = new List<IHtmlContent>();
    headers.Add(th(i("index")));
    headers.AddRange(df.Columns.Select(c => (IHtmlContent) th(c.Name)));
    var rows = new List<List<IHtmlContent>>();
    var take = 100;
    for (var i = 0; i < Math.Min(take, df.Rows.Count); i++)
    {
        var cells = new List<IHtmlContent>();
        cells.Add(td(i));
        foreach (var obj in df.Rows[i])
        {
            cells.Add(td(obj));
        }
        rows.Add(cells);
    } 
    var t = table(
        thead(
            headers),
        tbody(
            rows.Select(
                r => tr(r))));
    writer.Write(t);    
    writer.Write(df.Rows.Count + " x "+df.Columns.Count);
}, "text/html");
 
Formatter<DataFrameRow>.Register((dataFrameRow, writer) =>
{
    var cells = new List<IHtmlContent>();
    cells.Add(td(i));
    foreach (var obj in dataFrameRow)
    {
        cells.Add(td(obj));
    }
    var t = table(
        tbody(
            cells));
    writer.Write(t);
}, "text/html");
(3,1): error CS0103: 当前上下文中不存在名称“Formatter”

(30,1): error CS0103: 当前上下文中不存在名称“Formatter”

using Microsoft.Data.Analysis;

var start = new DateTime(2009,1,1);
Random rand = new Random();
var numDataPoint = 200;
PrimitiveDataFrameColumn<DateTime> date = new PrimitiveDataFrameColumn<DateTime>("Date", 
    Enumerable.Range(0, numDataPoint)
          .Select(offset => start.AddDays(offset))
          .ToList()); 
PrimitiveDataFrameColumn<int> data = new PrimitiveDataFrameColumn<int>("Data",
    Enumerable.Range(0, numDataPoint)
                        .Select(r => rand.Next(100))
                        .ToList()); 
var df = new DataFrame(date, data);
df
DateData\r\n2009/1/1 0:00:0020 \r\n2009/1/2 0:00:009 \r\n2009/1/3 0:00:0085 \r\n2009/1/4 0:00:0020 \r\n2009/1/5 0:00:0011 \r\n2009/1/6 0:00:0019 \r\n2009/1/7 0:00:0092 \r\n2009/1/8 0:00:0080 \r\n2009/1/9 0:00:0095 \r\n2009/1/10 0:00:0046 \r\n2009/1/11 0:00:0092 \r\n2009/1/12 0:00:0023 \...
Columns
indextypevalue
0Microsoft.Data.Analysis.PrimitiveDataFrameColumn<System.DateTime>
[ 2009/1/1 0:00:00, 2009/1/2 0:00:00, 2009/1/3 0:00:00, 2009/1/4 0:00:00, 2009/1/5 0:00:00, 2009/1/6 0:00:00, 2009/1/7 0:00:00, 2009/1/8 0:00:00, 2009/1/9 0:00:00, 2009/1/10 0:00:00, 2009/1/11 0:00:00, 2009/1/12 0:00:00, 2009/1/13 0:00:00, 2009/1/14 0:00:00, 2009/1/15 0:00:00, 2009/1/16 0:00:00, 2009/1/17 0:00:00, 2009/1/18 0:00:00, 2009/1/19 0:00:00, 2009/1/20 0:00:00 ... (more) ]
NullCount
0
Length
200
Name
Date
DataTypeSystem.DateTime
(values)
[ 2009/1/1 0:00:00, 2009/1/2 0:00:00, 2009/1/3 0:00:00, 2009/1/4 0:00:00, 2009/1/5 0:00:00, 2009/1/6 0:00:00, 2009/1/7 0:00:00, 2009/1/8 0:00:00, 2009/1/9 0:00:00, 2009/1/10 0:00:00, 2009/1/11 0:00:00, 2009/1/12 0:00:00, 2009/1/13 0:00:00, 2009/1/14 0:00:00, 2009/1/15 0:00:00, 2009/1/16 0:00:00, 2009/1/17 0:00:00, 2009/1/18 0:00:00, 2009/1/19 0:00:00, 2009/1/20 0:00:00 ... (more) ]
1Microsoft.Data.Analysis.PrimitiveDataFrameColumn<System.Int32>
[ 20, 9, 85, 20, 11, 19, 92, 80, 95, 46, 92, 23, 42, 46, 24, 67, 51, 17, 97, 51 ... (more) ]
NullCount
0
Length
200
Name
Data
DataTypeSystem.Int32
(values)
[ 20, 9, 85, 20, 11, 19, 92, 80, 95, 46, 92, 23, 42, 46, 24, 67, 51, 17, 97, 51 ... (more) ]
Rows
[ [ 2009/1/1 0:00:00, 20 ], [ 2009/1/2 0:00:00, 9 ], [ 2009/1/3 0:00:00, 85 ], [ 2009/1/4 0:00:00, 20 ], [ 2009/1/5 0:00:00, 11 ], [ 2009/1/6 0:00:00, 19 ], [ 2009/1/7 0:00:00, 92 ], [ 2009/1/8 0:00:00, 80 ], [ 2009/1/9 0:00:00, 95 ], [ 2009/1/10 0:00:00, 46 ], [ 2009/1/11 0:00:00, 92 ], [ 2009/1/12 0:00:00, 23 ], [ 2009/1/13 0:00:00, 42 ], [ 2009/1/14 0:00:00, 46 ], [ 2009/1/15 0:00:00, 24 ], [ 2009/1/16 0:00:00, 67 ], [ 2009/1/17 0:00:00, 51 ], [ 2009/1/18 0:00:00, 17 ], [ 2009/1/19 0:00:00, 97 ], [ 2009/1/20 0:00:00, 51 ] ... (more) ]
Count
200
(values)
indexvalue
0
[ 2009/1/1 0:00:00, 20 ]
(values)
indextypevalue
0System.DateTime2009-01-01 00:00:00Z
1System.Int32
20
1
[ 2009/1/2 0:00:00, 9 ]
(values)
indextypevalue
0System.DateTime2009-01-02 00:00:00Z
1System.Int32
9
2
[ 2009/1/3 0:00:00, 85 ]
(values)
indextypevalue
0System.DateTime2009-01-03 00:00:00Z
1System.Int32
85
3
[ 2009/1/4 0:00:00, 20 ]
(values)
indextypevalue
0System.DateTime2009-01-04 00:00:00Z
1System.Int32
20
4
[ 2009/1/5 0:00:00, 11 ]
(values)
indextypevalue
0System.DateTime2009-01-05 00:00:00Z
1System.Int32
11
5
[ 2009/1/6 0:00:00, 19 ]
(values)
indextypevalue
0System.DateTime2009-01-06 00:00:00Z
1System.Int32
19
6
[ 2009/1/7 0:00:00, 92 ]
(values)
indextypevalue
0System.DateTime2009-01-07 00:00:00Z
1System.Int32
92
7
[ 2009/1/8 0:00:00, 80 ]
(values)
indextypevalue
0System.DateTime2009-01-08 00:00:00Z
1System.Int32
80
8
[ 2009/1/9 0:00:00, 95 ]
(values)
indextypevalue
0System.DateTime2009-01-09 00:00:00Z
1System.Int32
95
9
[ 2009/1/10 0:00:00, 46 ]
(values)
indextypevalue
0System.DateTime2009-01-10 00:00:00Z
1System.Int32
46
10
[ 2009/1/11 0:00:00, 92 ]
(values)
indextypevalue
0System.DateTime2009-01-11 00:00:00Z
1System.Int32
92
11
[ 2009/1/12 0:00:00, 23 ]
(values)
indextypevalue
0System.DateTime2009-01-12 00:00:00Z
1System.Int32
23
12
[ 2009/1/13 0:00:00, 42 ]
(values)
indextypevalue
0System.DateTime2009-01-13 00:00:00Z
1System.Int32
42
13
[ 2009/1/14 0:00:00, 46 ]
(values)
indextypevalue
0System.DateTime2009-01-14 00:00:00Z
1System.Int32
46
14
[ 2009/1/15 0:00:00, 24 ]
(values)
indextypevalue
0System.DateTime2009-01-15 00:00:00Z
1System.Int32
24
15
[ 2009/1/16 0:00:00, 67 ]
(values)
indextypevalue
0System.DateTime2009-01-16 00:00:00Z
1System.Int32
67
16
[ 2009/1/17 0:00:00, 51 ]
(values)
indextypevalue
0System.DateTime2009-01-17 00:00:00Z
1System.Int32
51
17
[ 2009/1/18 0:00:00, 17 ]
(values)
indextypevalue
0System.DateTime2009-01-18 00:00:00Z
1System.Int32
17
18
[ 2009/1/19 0:00:00, 97 ]
(values)
indextypevalue
0System.DateTime2009-01-19 00:00:00Z
1System.Int32
97
19
[ 2009/1/20 0:00:00, 51 ]
(values)
indextypevalue
0System.DateTime2009-01-20 00:00:00Z
1System.Int32
51
... (more)
using XPlot.Plotly;
using Microsoft.Data.Analysis;
using System.Linq;
using Microsoft.AspNetCore.Html;

var chart1 = Chart.Plot(
    new Graph.Scatter
    {
        x = df1.Columns["Date"],
        y = df1.Columns["Open"],        
        mode = "lines+markers"
    }
);
var chart1_layout = new Layout.Layout{
    title="Open Price",
    xaxis =new Graph.Xaxis{
        title = "Date"
        },
    yaxis =new Graph.Yaxis{
    title = "Price (USD)"
        }           
    };
chart1.WithLayout(chart1_layout);
chart1
(7,9): error CS0246: 未能找到类型或命名空间名“Graph”(是否缺少 using 指令或程序集引用?)

(9,13): error CS0103: 当前上下文中不存在名称“df1”

(10,13): error CS0103: 当前上下文中不存在名称“df1”

(16,16): error CS0246: 未能找到类型或命名空间名“Graph”(是否缺少 using 指令或程序集引用?)

(19,16): error CS0246: 未能找到类型或命名空间名“Graph”(是否缺少 using 指令或程序集引用?)